A sort changes the order of the items in a data series. Ascending, descending or custom sort orders can be used. The user can sort individual data series or a collection of data series.
Sorting Individual Data Series
Two methods of the DataSeries class help sort the items of an individual data series. The first variant of the Sort method receives one parameter of type DataSeriesSortOrder, which has the following fields (for details, see DataSeriesSortOrder Enumeration):
Ascending
Descending
The following example sorts the Values data series of a bar series:
VB.NET |
|
bar.Values.Sort(DataSeriesSortOrder.Ascending) |
C# |
|
bar.Values.Sort(DataSeriesSortOrder.Ascending); |
It is important to remember that only a data series of type Double or String can be sorted with the first variant of the Sort method. If you attempt to perform the default Sort operation on a data series with different data type, the function will throw an exception.
The second variant of the sort method lets you specify a custom compare class. The following example implements a custom sort on the fill effect data series. In this example a fill effect is considered greater than another fill effect if the sum of its solid color RGB values is bigger.
VB.NET |
|
Public Class CustomComparer Implements IComparer Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare Dim feA As FillEffect,feB As FillEffect feA = CType(a, FillEffect) feB = CType(b, FillEffect) Dim aRGBSum As Integer = feA.Color.R + feA.Color.G + feA.Color.B Dim bRGBSum As Integer = feB.Color.R + feB.Color.G + feB.Color.B If aRGBSum < bRGBSum Then Return -1 Else If aRGBSum > bRGBSum Then Return 1 End If End If Return 0 End Function End Class ... Dim customComparer As CustomComparer = New CustomComparer() bar.Appearance.FillEffects.Sort(DataSeriesSortOrder.Ascending, customComparer) ...
|
C# |
|
public class CustomComparer : IComparer { public int Compare(object a, object b) { FillEffect feA, feB;
feA = (FillEffect)a; feB = (FillEffect)b;
int aRGBSum = feA.Color.R + feA.Color.G + feA.Color.B; int bRGBSum = feB.Color.R + feB.Color.G + feB.Color.B;
if (aRGBSum < bRGBSum) { return -1; } else if (aRGBSum > bRGBSum) { return +1; }
return 0; } };
...
CustomComparer customComparer = new CustomComparer(); bar.Appearance.FillEffects.Sort(DataSeriesSortOrder.Ascending, customComparer);
...
|
Sorting Collections of Data Series
The Sort methods of the DataSeriesCollection class allow a set of aligned data series to be sorted simultaneously. The sort is performed on one of the data series in the collection called "master data series". The user specifies the index of the master data series with the MasterDataSeries property, which is by default set to 0 (the first data series in the collection). The following example creates a data series collection holding the values, labels, and fill effects of a bar series.
VB.NET |
|
Dim dataSeriesCollection As DataSeriesCollection = New DataSeriesCollection() dataSeriesCollection.Add(bar.Values) dataSeriesCollection.Add(bar.Labels) dataSeriesCollection.Add(bar.Appearance.FillEffects)
|
C# |
|
DataSeriesCollection dataSeriesCollection = new DataSeriesCollection(); dataSeriesCollection.Add(bar.Values); dataSeriesCollection.Add(bar.Labels); dataSeriesCollection.Add(bar.Appearance.FillEffects); |
Now that the collection has been created, the user can perform default sorts on the value or label data series:
VB.NET |
|
' sort all data series in the collection on the values dataSeriesCollection.MasterDataSeries = 0 dataSeriesCollection.Sort(DataSeriesSortOrder.Ascending) ' sort all data series in the collection on the labels dataSeriesCollection.MasterDataSeries = 1 dataSeriesCollection.Sort(DataSeriesSortOrder.Ascending)
|
C# |
|
// sort all data series in the collection on the values dataSeriesCollection.MasterDataSeries = 0; dataSeriesCollection.Sort(DataSeriesSortOrder.Ascending); // sort all data series in the collection on the labels dataSeriesCollection.MasterDataSeries = 1; dataSeriesCollection.Sort(DataSeriesSortOrder.Ascending); |
If the user wants to sort the collection on the fill effect data series, the second overload of the sort operation must be specified and a custom compare object must be provided. The following code will sort all data series of the fill effects by using the custom compare, which was previously implemented.
VB.NET |
|
Dim customComparer As CustomComparer = New CustomComparer() dataSeriesCollection.MasterDataSeries = 2 dataSeriesCollection.Sort(DataSeriesSortOrder.Ascending, customComparer)
|
C# |
|
CustomComparer customComparer = new CustomComparer(); dataSeriesCollection.MasterDataSeries = 2; dataSeriesCollection.Sort(DataSeriesSortOrder.Ascending, customComparer);
|
Related Examples
Windows Forms: Data Manipulation\Sorting
See Also
DataSeries | DataSeriesCollection