Skip to main content
Row

PivotField.SortItems(PivotFieldSortType, Int32, IEnumerable<PivotItemReference>) Method

Sorts items in the current pivot field based on values in the specified data field.

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Spreadsheet.v23.2.Core.dll

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

void SortItems(
    PivotFieldSortType sortType,
    int dataFieldIndex,
    IEnumerable<PivotItemReference> itemReferences
)

Parameters

Name Type Description
sortType PivotFieldSortType

One of the PivotFieldSortType enumeration values specifying the sort order.

dataFieldIndex Int32

An integer value that is the zero-based index of the data field in the PivotDataFieldCollection collection to sort by.

itemReferences IEnumerable<PivotItemReference>

A collection of PivotItemReference objects representing references to the report’s items that specify a row or column in the data area on which to base sorting.

Remarks

Use the SortItems method to sort items in a row or column field by data field values. The PivotItemReference objects allow you to specify references to specific items in the report that describe a particular row or column in the data area containing values to sort by.

If the itemReferences parameter is not specified, items in the field will be sorted by their grand total values, as shown in the example below.

View Example

Dim worksheet As Worksheet = workbook.Worksheets("Report1")
workbook.Worksheets.ActiveWorksheet = worksheet

' Access the pivot table by its name in the collection.
Dim pivotTable As PivotTable = worksheet.PivotTables("PivotTable1")
' Access the pivot field by its name in the collection.
Dim field As PivotField = pivotTable.Fields("Product")
' Sort items in the "Product" field by values of the "Sum of Sales" data field.
field.SortItems(PivotFieldSortType.Ascending, 0)

The following image shows the result of the code’s execution.

PivotField_Sorting_SortByDataField

If you try to apply sorting to a field that is not currently shown as a row or column field in the report, or sort settings you specify are invalid, an exception occurs. To view or modify sorting criteria, use the PivotField.Sort property.

To sort items in a specific field in ascending or descending order, use the PivotField.SortType property.

See Also