Sort Data in Worksheets
- 4 minutes to read
The Spreadsheet Document API allows you to sort a cell range by one or more columns. You can sort by cell value, cell fill color, or font color, and combine multiple sort fields into a single sort operation.
Note
CellRange.Sort(columnOffset, ascending) and Worksheet.Sort(range, columnOffset, ascending) methods do not detect a header row and sort all rows in the range. If the range includes a header row or a total row, exclude these rows from the range. To sort table data rows, call the Table.Sort(columnOffset, ascending) method. The Table.Sort method does not move the table header row.
Sort a Range by a Single Column
Call the CellRange.Sort(columnOffset, ascending) method to sort a range by a single column. The columnOffset parameter is a zero-based offset from the first column in the range. The ascending parameter specifies the sort order.
The following code snippet sorts data rows (the A2:C3 range without the header row and the total row) by the second column (column B) in descending order:
using System.IO;
using DevExpress.Docs.Spreadsheet;
// Load a workbook from a file.
using FileStream input = File.OpenRead("Document.xlsx");
using Workbook workbook = new Workbook(input);
// Access the first worksheet in the workbook.
Worksheet worksheet = workbook.Worksheets[0];
// Sort the range by the second column in descending order.
worksheet["A2:C3"].Sort(columnOffset: 1, ascending: false);
Directory.CreateDirectory("output");
using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream);
To pass the range as a parameter, call the Worksheet.Sort(range, columnOffset, ascending) method.
Sort by Multiple Fields
To sort a range by multiple columns, pass a list of SortField descendants to the CellRange.Sort(sortFields) method. The API sorts rows by the first field and uses each subsequent field to order rows with equal values in previous fields.
The following sort field types are available:
| Sort field | Description |
|---|---|
| ValueSortField | A sort field that sorts rows by cell values. |
| FillSortField | A sort field that sorts rows by cell fill. |
| FontColorSortField | A sort field that sorts rows by font color. |
Use the SortField.ColumnOffset property to specify the column to sort by and the SortField.Descending property to specify the sort order. If the Descending property of a FillSortField or FontColorSortField object is true, rows with the specified color move to the bottom of the range instead of the top.
The following code snippet sorts a range first by the first column in ascending order, then by the third column in descending order:
worksheet["A2:C3"].Sort(new SortField[] {
new ValueSortField(columnOffset: 0, descending: false),
new ValueSortField(columnOffset: 2, descending: true)
});
Sort by Cell Fill or Font Color
The following code snippet sorts a range so that rows with a yellow fill in the first column appear at the top:
using System.Drawing;
worksheet["A2:C3"].Sort(new SortField[] {
new FillSortField(columnOffset: 0, fill: new SolidFillFormat(CellColor.FromColor(Color.Yellow)))
});
To sort rows by font color, use the FontColorSortField class. The following code snippet sorts a range so that rows with a red font color in the first column appear at the top:
worksheet["A2:C3"].Sort(new SortField[] {
new FontColorSortField(columnOffset: 0, color: CellColor.FromColor(Color.Red))
});
Sort with a Custom Comparer
To apply custom sort logic (for example, a case-sensitive or culture-specific text comparison), assign an IComparer<CellValue> implementation to the ValueSortField.Comparer property. The following code snippet sorts text values in the first column with a case-sensitive ordinal comparison: