Click or drag to resize

ExcelWorksheetSort Property

Gets or sets the active SortState. Returns if no SortState is active.

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

Active SortState is loaded from and saved to XLSX file.

Namespace:  GemBox.Spreadsheet
Assembly:  GemBox.Spreadsheet (in GemBox.Spreadsheet.dll) Version: 43.0.35.1036
Syntax
public SortState Sort { get; set; }

Property Value

Type: SortState
The active SortState or if no SortState is active.
Exceptions
ExceptionCondition
ArgumentExceptionSort range doesn't belong to this worksheet.
Remarks
Use Sort(Boolean) method to create or get a SortState instance.
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