Skip to main content
Row

Worksheet.Sort(CellRange, Int32, IComparer<CellValue>) Method

Sorts the specified range by the specified column using the specified comparer.

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Spreadsheet.v23.2.Core.dll

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

void Sort(
    CellRange range,
    int columnOffset,
    IComparer<CellValue> comparer
)

Parameters

Name Type Description
range CellRange

A CellRange object that is the range of cells in a worksheet.

columnOffset Int32

An integer that is the offset of the column by which to sort from the first column in the range. The first column has the offset 0.

comparer IComparer<CellValue>

An IComparer<T><CellValue,>

Remarks

You can specify a default built-in comparer - the Worksheet.Comparers.Ascending is used for sorting in ascending order, the Worksheet.Comparers.Descending is used for sorting in descending order.

Implementing a custom comparer can give you more power and flexibility. This technique is illustrated in the code snippet below.

A custom comparer implements the IComparer<T><CellValue,> interface. The sample comparer in the following code compares string length for text data, so that greater length means greater value. Thus, the sort method will sort the column by text length in ascending order.

Sort_CustomComparer

View Example

Dim worksheet As Worksheet = workbook.Worksheets(0)

' Fill in the range.
worksheet.Cells("A2").Value = "Donald Dozier Bradley"
worksheet.Cells("A3").Value = "Tony Charles Mccallum-Geteer"
worksheet.Cells("A4").Value = "Calvin Liu"
worksheet.Cells("A5").Value = "Anita A Boyd"
worksheet.Cells("A6").Value = "Angela R. Scott"
worksheet.Cells("A7").Value = "D Fox"

' Sort values using a custom comparer.
Dim range As CellRange = worksheet.Range("A2:A7")
worksheet.Sort(range, 0, New SampleComparer())

' Create a heading.
Dim header As CellRange = worksheet.Range("A1")
header(0).Value = "Use a custom comparer"
header.ColumnWidthInCharacters = 30
header.Style = workbook.Styles("Heading 1")

The following code snippets (auto-collected from DevExpress Examples) contain references to the Sort(CellRange, Int32, IComparer<CellValue>) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also