Click or drag to resize

SortState Class

Represents sort settings and provides sort functionality.
Inheritance Hierarchy
SystemObject
  GemBox.SpreadsheetSortState

Namespace:  GemBox.Spreadsheet
Assembly:  GemBox.Spreadsheet (in GemBox.Spreadsheet.dll) Version: 41.3.30.1132
Syntax
public sealed class SortState

The SortState type exposes the following members.

Properties
  NameDescription
Public propertyCaseSensitive

Gets or sets a value indicating whether the sort is case-sensitive.

Default value is .

Public propertyLeftToRight

Gets or sets a value indicating whether to sort from left to right (whether to sort columns instead of rows).

Default value is .

Public propertyLevels
Gets the sort levels.
Public propertyRange
Gets the range to sort.
Top
Methods
  NameDescription
Public methodApply
Performs the sorting of this Range based on this SortState settings.
Public methodBy(Int32)
Adds a SortLevel applied to a column (or a row, if LeftToRight is ) with the specified index (relative to the Range).
Public methodBy(Int32, Boolean)
Adds a SortLevel applied to a column (or a row, if LeftToRight is ) with the specified index (relative to the Range) and with the specified order to sort by.
Public methodBy(Int32, IEnumerableString)
Adds a SortLevel applied to a column (or a row, if LeftToRight is ) with the specified index (relative to the Range) and with the specified custom list by which order of items to sort by.
Public methodBy(Int32, ComparisonExcelCell)
Adds a SortLevel applied to a column (or a row, if LeftToRight is ) with the specified index (relative to the Range) and with the specified comparison method to sort by.
Public methodBy(Int32, String)
Adds a SortLevel applied to a column (or a row, if LeftToRight is ) with the specified index (relative to the Range) and with the specified custom list by which order of items to sort by.
Public methodBy(Int32, Boolean, IEnumerableString)
Adds a SortLevel applied to a column (or a row, if LeftToRight is ) with the specified index (relative to the Range) and with the specified order and custom list by which order of items to sort by.
Public methodBy(Int32, Boolean, String)
Adds a SortLevel applied to a column (or a row, if LeftToRight is ) with the specified index (relative to the Range) and with the specified order and custom list by which order of items to sort by.
Public methodToString
Returns a String that represents this SortState instance.
(Overrides ObjectToString.)
Top
Remarks

Use Sort(Boolean) method to create or get a SortState instance.

Active SortState is stored in the parent worksheet (Sort) and is loaded from and saved to XLSX file.

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