Chart for .NET / User's Guide / Data Manipulation / General Operations / Sorting

In This Topic
    Sorting
    In This Topic

    Sorting changes the order of the data series items according to some criterion. Ascending, descending or custom sort orders can be used. The user can sort individual data series or collection of data series.

     Sorting individual data series

    Two methods of the NDataSeries class can help you sort the items of an individual data series. The first variant of the Sort method receives a parameter of type DataSeriesSortOrder which has the following fields:

    Ascending
    Descending

    The following example will sort the Values data series of a bar series:

    C#
    Copy Code
    bar.Values.Sort(DataSeriesSortOrder.Ascending);
    
    Visual Basic
    Copy Code
    bar.Values.Sort(DataSeriesSortOrder.Ascending)
    

    It is important to remember that only the 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 comparer class. For example you can implement a custom comparer for complex types as fill styles.

     Sorting collections of data series

    The Sort methods of the NDataSeriesCollection class allow a set of aligned data series to be sorted simultaneously. One of the data series in the collection is used as a key for sorting. The other data series are reordered accordingly. The following code will create a data series collection containing the values, labels and fill styles of a series.

    C#
    Copy Code
    NDataSeriesCollection dataSeriesCollection = new NDataSeriesCollection();
    dataSeriesCollection.Add(series.Values);
    dataSeriesCollection.Add(series.Labels);
    dataSeriesCollection.Add(series.FillStyles);
    
    Visual Basic
    Copy Code
    Dim dataSeriesCollection As NDataSeriesCollection = New NDataSeriesCollection()
    dataSeriesCollection.Add(series.Values)
    dataSeriesCollection.Add(series.Labels)
    dataSeriesCollection.Add(series.FillStyles)
    

    Now that the collection is created you can perform sorting on the values or labels data series:

    C#
    Copy Code
    // sort all data series in the collection on the values
    dataSeriesCollection.Sort(0, DataSeriesSortOrder.Ascending);
    
    // sort all data series in the collection on the labels
    dataSeriesCollection.Sort(1, DataSeriesSortOrder.Ascending);
    
    Visual Basic
    Copy Code
    ' sort all data series in the collection on the values
    dataSeriesCollection.Sort(0, DataSeriesSortOrder.Ascending)
    
    ' sort all data series in the collection on the labels
    dataSeriesCollection.Sort(1, DataSeriesSortOrder.Ascending)
    

    If you want to sort the collection using the fill styles as a key, you have to use the second overload of the Sort method and specify a custom comparer.

     Related Examples
    Windows forms: All Examples\Data Manipulation\Sorting
    See Also