New to Telerik UI for WPF? Download free 30-day trial

Sorting

In this article you will find out how to sort your data based on the header names or the totals.

Understanding Sorting Mechanism

RadPivotGrid provides different sorting capabilities. You can sort your RowGroupDescriptions and ColumnGroupDescriptions ascending (from A to Z) and descending (from Z to A) based on the names of the properties they are showing or based on the aggregated description.

With the Q1 2014 SP1 version of Telerik UI for RadPivotGrid provides an additional option. The data can be shown the way it was received from the data source using the SortOrder property set to None.

We will explain this with a simple RadPivotGrid:

Rad Pivot Grid Features Queryable Sorting 01

The ColumnGroupDescription in this RadPivotGrid is the OrderDate. By default the sorting of the columns is ascending (from A to Z) based on the headers: 1996, 1997, 1998. You can easily change the sorting of the OrderDate ColumnGroupDescription and set it to descending (from Z to A). This will lead to the following result:

Rad Pivot Grid Features Queryable Sorting 02

The columns are reordered based on their names (check the green rectangle on the top of the image). You can apply sorting based on some of the QueryablePropertyAggregateDescription you are using. In the example above you can apply sorting of the OrderDate ColumnGroupDescription in ascending (from A to Z) or descending (from Z to A) format based on the Total Sum of Freight (red rectangle) or Total Sum of ShipVia (blue rectangle). Here is the new view of RadPivotGrid when the OrderDate ColumnGroupDescription is sorted in descending format based on the Total Sum of Freight:

Rad Pivot Grid Features Queryable Sorting 03

The sorting is set on the ColumnGroupDescriptions or RowGroupDescriptions. You cannot set sorting on the AggregateDescriptions, but you can sort the columns or rows based on the aggregated values.

Sort the data

The sorting can be applied in the XAML, in the code behind or even at runtime with the help of RadPivotFieldList.

  • Sorting based on the GroupName (header) can be Ascending (from A to Z) or Descending (from Z to A). The default value is Ascending, but you can change it by using SortOrder property:

<pivot:QueryablePropertyGroupDescription PropertyName="ShipCountry" SortOrder="Ascending"/> 
<pivot:QueryableDoubleGroupDescription PropertyName="Freight" SortOrder="Descending"/> 
<pivot:QueryableDateTimeGroupDescription PropertyName="OrderDate" SortOrder="Ascending"/> 

var shipCountryGroupDescription = new QueryablePropertyGroupDescription(); 
shipCountryGroupDescription.PropertyName = "ShipCountry"; 
shipCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Ascending; 
 
var freightCountryGroupDescription = new QueryableDoubleGroupDescription(); 
freightCountryGroupDescription.PropertyName = "ShipCountry"; 
freightCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Descending; 
 
var orderDateCountryGroupDescription = new QueryableDateTimeGroupDescription(); 
orderDateCountryGroupDescription.PropertyName = "OrderDate"; 
orderDateCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Ascending; 
Dim shipCountryGroupDescription = New QueryablePropertyGroupDescription() 
shipCountryGroupDescription.PropertyName = "ShipCountry" 
shipCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Ascending 
 
Dim freightCountryGroupDescription = New QueryableDoubleGroupDescription() 
freightCountryGroupDescription.PropertyName = "ShipCountry" 
freightCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Descending 
 
Dim orderDateCountryGroupDescription = New QueryableDateTimeGroupDescription() 
orderDateCountryGroupDescription.PropertyName = "OrderDate" 
orderDateCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Ascending 
  • If you need to avoid the data to be sorted anyhow, you can set the SortOrder property to None. This way the data will appear the way it is was received from the data source:

<pivot:QueryablePropertyGroupDescription PropertyName="ShipCountry" SortOrder="None"/> 
<pivot:QueryableDoubleGroupDescription PropertyName="Freight" SortOrder="None"/> 
<pivot:QueryableDateTimeGroupDescription PropertyName="OrderDate" SortOrder="None"/> 

var shipCountryGroupDescription = new QueryablePropertyGroupDescription(); 
shipCountryGroupDescription.PropertyName = "ShipCountry"; 
shipCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.None; 
 
var freightCountryGroupDescription = new QueryableDoubleGroupDescription(); 
freightCountryGroupDescription.PropertyName = "ShipCountry"; 
freightCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.None; 
 
var orderDateCountryGroupDescription = new QueryableDateTimeGroupDescription(); 
orderDateCountryGroupDescription.PropertyName = "OrderDate"; 
orderDateCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.None; 
Dim shipCountryGroupDescription = New QueryablePropertyGroupDescription() 
shipCountryGroupDescription.PropertyName = "ShipCountry" 
shipCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.None 
 
Dim freightCountryGroupDescription = New QueryableDoubleGroupDescription() 
freightCountryGroupDescription.PropertyName = "ShipCountry" 
freightCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.None 
 
Dim orderDateCountryGroupDescription = New QueryableDateTimeGroupDescription() 
orderDateCountryGroupDescription.PropertyName = "OrderDate" 
orderDateCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.None 

Note that the SortOrder None property is available with the Q1 2014 SP1 version of Telerik UI for WPF.

  • Sorting based on the GrandTotals can be Ascending or Descending. To set such sort mechanism you have to use the SortOrder and GroupComparer properties:

<pivot:QueryablePropertyGroupDescription PropertyName="ShipCountry" SortOrder="Ascending"> 
    <pivot:QueryablePropertyGroupDescription.GroupComparer> 
        <pivot:GrandTotalComparer AggregateIndex="0"/> 
    </pivot:QueryablePropertyGroupDescription.GroupComparer> 
</pivot:QueryablePropertyGroupDescription> 
<pivot:QueryableDoubleGroupDescription PropertyName="Freight" SortOrder="Descending"> 
    <pivot:QueryablePropertyGroupDescription.GroupComparer> 
        <pivot:GrandTotalComparer AggregateIndex="1"/> 
    </pivot:QueryablePropertyGroupDescription.GroupComparer> 
</pivot:QueryableDoubleGroupDescription> 
<pivot:QueryableDateTimeGroupDescription PropertyName="OrderDate" SortOrder="Ascending"> 
    <pivot:QueryablePropertyGroupDescription.GroupComparer> 
        <pivot:GrandTotalComparer AggregateIndex="0"/> 
    </pivot:QueryablePropertyGroupDescription.GroupComparer> 
</pivot:QueryableDateTimeGroupDescription> 

var shipCountryGroupDescription = new QueryablePropertyGroupDescription(); 
shipCountryGroupDescription.PropertyName = "ShipCountry"; 
shipCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Ascending; 
shipCountryGroupDescription.GroupComparer = new GrandTotalComparer() { AggregateIndex = 0 }; 
 
var freightCountryGroupDescription = new QueryableDoubleGroupDescription(); 
freightCountryGroupDescription.PropertyName = "Freight"; 
freightCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Descending; 
freightCountryGroupDescription.GroupComparer = new GrandTotalComparer() { AggregateIndex = 1 }; 
 
var orderDateCountryGroupDescription = new QueryableDateTimeGroupDescription(); 
orderDateCountryGroupDescription.PropertyName = "OrderDate"; 
orderDateCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Ascending; 
orderDateCountryGroupDescription.GroupComparer = new GrandTotalComparer() { AggregateIndex = 0 }; 
Dim shipCountryGroupDescription = New QueryablePropertyGroupDescription() 
shipCountryGroupDescription.PropertyName = "ShipCountry" 
shipCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Ascending 
shipCountryGroupDescription.GroupComparer = New GrandTotalComparer() With {.AggregateIndex = 0} 
 
Dim freightCountryGroupDescription = New QueryableDoubleGroupDescription() 
freightCountryGroupDescription.PropertyName = "Freight" 
freightCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Descending 
freightCountryGroupDescription.GroupComparer = New GrandTotalComparer() With {.AggregateIndex = 1} 
 
Dim orderDateCountryGroupDescription = New QueryableDateTimeGroupDescription() 
orderDateCountryGroupDescription.PropertyName = "OrderDate" 
orderDateCountryGroupDescription.SortOrder = Telerik.Pivot.Core.SortOrder.Ascending 
orderDateCountryGroupDescription.GroupComparer = New GrandTotalComparer() With {.AggregateIndex = 0} 

The AggregateIndex property is set based on the count of your aggregate descriptions. If you have two aggregates the first one will have AggregateIndex = 0 and the second - AggregateIndex = 1.

Changing the Sorting at runtime

RadPivotFieldList gives you the ability to change the sorting of a description at runtime. When you click on RowGroupDescription or ColumnGroupDescription a new popup opens with several sorting options. If you want to save the current sorting, but only change its order (for example the above RadPivotGrid is sorted based on the Total Sum of Quantity in Descending order, and you want to change it to Ascending) you can use the first two options which are doing exactly this - just changing the SortOrder:

Rad Pivot Grid Features Sorting 04

If you want to change the property based on which the sort is applied, then you can use the third option - More Sorting Options. A new RadWindow will be opened in which you can choose Ascending or Descending order of the sort. When you click the dropdown button you'll see a full list of the possible sorting basis:

Rad Pivot Grid Features Queryable Sorting 06

See Also

In this article