Overview
A sort changes the order of a series' data points, and is based on a data point value (by default this is the first Y value). Ascending, descending or custom sort orders can be used.
If more than one series is sorted using one method call then it is always sorted using values from the first listed series, and all series MUST be aligned, otherwise an exception will be thrown. For details concerning alignment see the Aligning Series topic.
Performing Ascending or Descending Sorts
Use the DataManipulator object's Sort method to sort data. An ascending or descending sort order can be used.
Example
This example demonstrates how to sort series data using the default first Y value.
Visual Basic |
Copy Code |
Imports Dundas.Charting.WinControl
...
' Sort series in ascending order.
mySeries.Sort(PointsSortOrder.Ascending)
' Sort series in descending order.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "MySeries")
|
C# |
Copy Code |
using Dundas.Charting.WinControl;
...
// Sort series in ascending order.
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 (Y2, Y3, ...) and also by their AxisLabel property.
Example
This example demonstrates how to sort series data using non-default point values.
Visual Basic |
Copy Code |
Imports Dundas.Charting.WinControl
...
' Sort series in ascending order by X value.
mySeries.Sort(PointsSortOrder.Ascending, "X")
' Sort series in ascending order by the second Y value.
mySeries.Sort(PointsSortOrder.Ascending, "Y2")
' Sort series in descending order by axis label.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "AxisLabel","MySeries")
|
C# |
Copy Code |
using Dundas.Charting.WinControl;
...
// Sort series in ascending order by X value.
mySeries.Sort(PointsSortOrder.Ascending, "X");
// Sort series in ascending order by the second Y value.
mySeries.Sort(PointsSortOrder.Ascending, "Y2");
// Sort series in descending order by axis label.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "AxisLabel", "MySeries");
|
Performing Custom Sorts
If more complex sorting logic is required use the IComparer interface. The Compare method of this interface receives two objects as parameters and when overridden 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.
Note: The two object parameters of the Compare method must always be of type DataPoint.
Example
This example demonstrates how to use the IComparer interface to provide custom sorting logic.
Visual Basic |
Copy Code |
Imports Dundas.Charting.WinControl
...
' 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
|
C# |
Copy Code |
using Dundas.Charting.WinControl;
...
// 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;
}
}
}
|
Sorting Multiple Series
The Sort method of the DataManipulator class allows for multiple aligned series to be sorted simultaneously by specifying a comma-separated list of series names. All points in all series will be sorted using the values from the first series listed. In other words, changes in the order of data points will be duplicated in all series, and are based on changes made to the first series.
If two series must be sorted independently, call the Sort method twice using one series as a parameter.
Example
This example demonstrates how to sort multiple series in descending order using the Sort function of the DataManipulator class.
Visual Basic |
Copy Code |
Imports Dundas.Charting.WinControl
...
' Sorting multiple series in descending order.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "ProductSales,ProductPrice")
|
C# |
Copy Code |
using Dundas.Charting.WinControl;
...
// Sorting multiple series in descending order.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "ProductSales,ProductPrice");
|
See Also