Click or drag to resize
CellRangeSort Method

Gets the SortState active on the parent worksheet (Sort) if active Range is equal to this CellRange and parameter active is ; otherwise, a new SortState instance, which is set as active on the parent worksheet if parameter active is .

SortState is used to store sort settings and to apply sort on this range of cells.

Namespace: GemBox.Spreadsheet
Assembly: GemBox.Spreadsheet (in GemBox.Spreadsheet.dll) Version: 41.3.30.1000
Syntax
public SortState Sort(
	bool active
)

Parameters

active
Type: SystemBoolean
if set to , gets or sets the SortState active on the parent worksheet (Sort).

Return Value

Type: SortState
The SortState active on the parent worksheet (Sort) if active Range is equal to this CellRange and parameter active is ; otherwise, a new SortState instance, which is set as active on the parent worksheet if parameter active is .
Remarks
Use this method (with parameter active set to ) to create multiple independent SortStates for the same CellRange instance, even if it has an active SortState (Sort), which can then be stored and used later on.
Examples

Following methods shows various ways to specify and apply a SortState to a range of cells.

static void Sort1(ExcelWorksheet worksheet)
{
    // Specify and apply Sort using less-verbose, fluent, but less understandable interface.
    worksheet.Cells.GetSubrange("A1", "D20").Sort(true).
        By(0).
        By(1, true).
        By(2, "Female", "Male").
        Apply();
}

static void Sort2(ExcelWorksheet worksheet)
{
    // Specify and apply Sort using more verbose, but more understandable interface.
    var sort = worksheet.Cells.GetSubrange("A1", "D20").Sort(true);

    sort.Levels.Add(0);

    var level2 = sort.Levels.Add(1);
    level2.Descending = true;

    var level3 = sort.Levels.Add(2);
    level3.SetCustomList("Female", "Male");

    sort.Apply();
}

static void Sort3(ExcelWorksheet worksheet)
{
    // Specify and apply Filter using less-verbose, fluent and understandable interface (with help of C# 4.0 named arguments).
    worksheet.Cells.GetSubrange("A1", "D20").Sort(active: true).
        By(columnRowIndex: 0).
        By(columnRowIndex: 1, descending: true).
        By(columnRowIndex: 2, customList: new string[] { "Female", "Male" }).
        Apply();
}
See Also