You can sort pivot tables by setting the PivotTable Field - `Sort` property.

```csharp
rf.Sort = eSortType.Ascending;
```

```vbnet
rf.Sort = eSortType.Ascending
```

This applies to column and row fields. You can also sort on data fields by using the `SetAutoSort` method.

```csharp
rowField.SetAutoSort(df, eSortType.Descending);
```

```vbnet
rowField.SetAutoSort(df, eSortType.Descending)
```

This method sets the `AutoSort` property that contains the pivot area to set for the pivot table field sorting.

If you have multiple row and column fields you can set the auto sort by using pivot areas. This sample sorts by the column field value "Hardware".

```csharp
var rowField = p1.RowFields.Add(p1.Fields[0]);
var columnField = p1.ColumnFields.Add(p1.Fields[1]);
var dataField = p1.DataFields.Add(p1.Fields[3]);
rf.SetAutoSort(df, eSortType.Descending);
var reference = rf.AutoSort.Conditions.Fields.Add(cf);
cf.Items.Refresh(); //You must refresh the items to fill the items collection with the correct values.
reference.Items.AddByValue("Hardware");
```

```vbnet
Dim rowField = p1.RowFields.Add(p1.Fields(0))
Dim columnField = p1.ColumnFields.Add(p1.Fields(1))
Dim dataField = p1.DataFields.Add(p1.Fields(3))
rf.SetAutoSort(df, eSortType.Descending)
Dim reference = rf.AutoSort.Conditions.Fields.Add(cf)
cf.Items.Refresh() 'You must refresh the items to fill the items collection with the correct values.
reference.Items.AddByValue("Hardware")
```

### See also

- [Pivot Table Sample 7.2](<https://github.com/EPPlusSoftware/EPPlus.Samples.CSharp/tree/master/07-Tables pivot tables and slicers/02-Pivot tables>) or [Pivot Table Sample 7.2-VB](<https://github.com/EPPlusSoftware/EPPlus.Samples.VB/blob/master/07-Tables pivot tables and slicers/02-Pivot tables>)
- [Pivot tables](<https://epplussoftware.com/docs/tables-and-pivot-tables/pivot-tables/index.md>)
