Sorting Data in Chart Controls

Sorting changes the order of a series' data points based on a data point value. You can use ascending, descending, or custom sort orders.

Performing Ascending or Descending Sorts

To sort data, use the Sort method in the Series or DataManipulator object. You can use an ascending or descending sort order.

The following code demonstrates how to sort series data using the default first Y value.

' Sort series in ascending order.
Chart1.Series("MySeries").Sort(PointsSortOrder.Ascending)

' Sort series in descending order.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "MySeries")
// Sort series in ascending order.
Chart1.Series["MySeries"].Sort(PointsSortOrder.Ascending);

// Sort series in descending order.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "MySeries");

By default the first Y value of the data points is used for sorting. However, you can sort data points using their X value, or any of their other Y values, such as Y2. You can also sort by each value's AxisLabel property.

The following code demonstrates how to sort series data using non-default point values.

' Sort series in ascending order by X value.
Chart1.Series("MySeries").Sort(PointsSortOrder.Ascending, "X")

' Sort series in descending order by axis label.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "AxisLabel","MySeries")
// Sort series in ascending order by X value.
Chart1.Series["MySeries"].Sort(PointsSortOrder.Ascending, "X");

// Sort series in descending order by axis label.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "AxisLabel", "MySeries");

Sorting Multiple Series

You can use DataManipulator.Sort to sort multiple aligned series by specifying a comma-separated list of series names. This method sorts all points in all series based on their corresponding values from the first series listed. In other words, it duplicates the changes in the order of data points in the first series to all series in the list.

Note

If you use DataManipulator.Sort to sort more than one series, the data from all series must be aligned. Otherwise, the method throws an exception. For more information on aligning data, see Aligning Data.

The following code demonstrates how to sort multiple series in descending order.

Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "ProductSales,ProductPrice")
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "ProductSales,ProductPrice");

Performing Custom Sorts

To define a custom sort, use the IComparer interface. The Compare method of this interface receives two DataPoint objects as arguments. It should return a value less than zero if the first object is less than the second, zero if they are equal, and a value greater than zero if the first object is greater than the second.

This example demonstrates how to use the IComparer interface to provide custom sorting logic.

' Sort series.
Chart1.DataManipulator.Sort(New CustomComparer(), "MySeries")
  
' Custom sorting comparing class.
Public Class CustomComparer Implements IComparer
   ' Compares two data points by their Labels and returns a value indicating
   ' if the first data point is less than, equal to, or greater than the second
   ' data point.
   Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
                Dim pointA As DataPoint = a
                Dim pointB As DataPoint = b
                ' Compares data points' Label property
                If pointA.Label < pointB.Label Then
                  Compare = -1
                ElseIf pointA.label > pointB.Label Then
                  Compare = 1
                Else
                  Compare = 0
                End If
        End Function
End Class
// Sort series.
Chart1.DataManipulator.Sort(new CustomComparer(), "MySeries");
  
// Custom sorting comparing class.
public class CustomComparer : IComparer {
    
    // Compares two data points by their Labels and returns a value indicating
    // if the first data point is less than, equal to, or greater than the second
    // data point.
    public int Compare(object a, object b) {
        DataPoint pointA = a;
        DataPoint pointB = b;
        // Compares data points' Label property
        if ((pointA.Label < pointB.Label)) {
            Compare = -1;
        }
        else if ((pointA.label > pointB.Label)) {
            Compare = 1;
        }
        else {
            Compare = 0;
        }
    }
}

See Also

Reference

System.Windows.Forms.DataVisualization.Charting

System.Web.UI.DataVisualization.Charting

Concepts

Filtering Data

Aligning Data

Grouping Data

Copying, Splitting, and Merging Data

Other Resources

Data Binding and Manipulation