﻿ Element Sorting
 .netCHARTING v9.3 Documentation Send comments on this topic. Element Sorting
 Getting Started > Data Tutorials > Data Manipulation > Element Sorting

Glossary Item Box

Introduction

This tutorial will describe how elements are sorted by default and how their sort order can be manipulated. If the elements have x axis values such as numbers or dates, sorting is not very useful because the sequential axis will determine their position, however, when elements use string names for the x axis sorting is much more useful.

Quantitative Axis Sorting

Elements with numeric or dateTime x values and a numeric or dateTime x axis will automatically be sorted sequentially and fall into place where the values appear on the axis. Elements may contain names as well as x values such as dates, however, if both names and values exist, the x axis will default to a category axis which shows names of the elements. To force the axis to use a time scale the following code can be used.

```[C#]
Chart.XAxis.Scale = Scale.Time;
```
```[Visual Basic]
Chart.XAxis.Scale = Scale.Time```

If the elements have names and numbers using Scale.Normal will have the same effect causing the x axis to become a numeric axis.
For more information on element values and axis scales see the Elements and Axis Scales tutorial.

 The chart mentor can be helpful in this case as it can detect your data types, axis scales and report back suggestions.

Category Axis Sorting

Single Series Default Sorting

How They Behave
A single series of elements with names instead of x values will be drawn on the chart in the order the elements are added to the series.

 TIP: To reverse the order of elements on an axis, it can be done quickly by using Chart.XAxis.InvertScale = true

Multiple Series Default Sorting

If multiple series are added to the chart with the same element names and in the same order the chart will draw them in the order they are added. When multiple series with elements that are different are added, the chart performs some smart grouping functionality that processes the sort order.This section will describe the element name grouping functionality.

As an example case, elements will be provided for the chart in a segmented order.

3 Series, each with 4 elements:

 Series 1: C D E F Series 2: E F G H Series 3: A B C D

SeriesCollection.SmartGrouping = true (Default)

Intended order:

 A B C D E F G H

.netCHARTING Will analyze this list and determine what the true intended order should be. This does not employ sorting, it looks for patterns in element orders of different series to determine the original order. In this case the available element names offer enough information to determine the sort order to be the above.

SeriesCollection.SmartGrouping = false

When false, the element order will be based on the orders in which series elements are provided. The above example will result in this order:

 C D E F G H A B

The first series defines the order to be CDEF then the second series has two new element names which are added at the end GH. The final series offers AB as new names and that will be added at the end and so on.

 Despite any sorting performed on a series, if the x axis is a category axis, the above processing will be applied.

Custom Sorting

Series.Sort (Sort Elements)

Elements within a series can be sorted by any value in ascending or descending order.

```[C#]
// Sort the series by name
s.Sort(ElementValue.Name, "DESC");
// Sort the series by YValue
s.Sort(ElementValue.YValue, "DESC");
```
```[Visual Basic]
' Sort the series by name
s.Sort(ElementValue.Name, "DESC")
' Sort the series by YValue
s.Sort(ElementValue.YValue, "DESC")
```

SeriesCollection.Sort (Sort Series)

Series within a SeriesCollection can also be sorted. This does not affect the element sort order, only the order of the series within the collection. The sorting is based on the sum of any element values within a series.

```[C#]
mySeriesCollection.Sort(ElementValue.YValue,"DESC");
```
```[Visual Basic]
mySeriesCollection.Sort(ElementValue.YValue,"DESC")
```

SeriesCollection.SortElementGroups (Sort Element Groups)

Element groups are elements belonging to different series but sharing a name. It is the same as the requirement for a stack chart's stacks. Only element groups can be stacked. In effect, this can be considered a way to sort the columns of a stacked chart.

```[C#]
mySeriesCollection.SortElementGroups(ElementValue.YValue,"ASC");
```
```[Visual Basic]
mySeriesCollection.SortElementGroups(ElementValue.YValue,"ASC")
```
 Sample: SortElementStacks.aspx