Table of Contents

Method Sort

Namespace
DocWright.Spreadsheet
Assembly
DocWright.Spreadsheet.dll

Sort(XlsxCellRange, params XlsxSortKey[])

Sorts the rows of a range by one or more of its columns, as Excel's Data > Sort does. The range has no header row; see Sort(XlsxCellRange, XlsxSortOptions?, params XlsxSortKey[]).

public void Sort(XlsxCellRange range, params XlsxSortKey[] keys)

Parameters

range XlsxCellRange

The cells sorted. Cells outside it do not move.

keys XlsxSortKey[]

The columns to sort by, the first deciding first.

Exceptions

ArgumentNullException

keys or one of them is null.

ArgumentException

No key is given, more than 64 are, a key's column is outside the range, or the range reaches outside the worksheet grid.

InvalidOperationException

The sheet is not a worksheet.

UnsupportedFeatureException

The rows sorted cross a merged range, part of an array formula, a table's header or totals row, or a filter that hides rows.

InvalidDocumentException

A part of the workbook is malformed.

Sort(XlsxCellRange, XlsxSortOptions?, params XlsxSortKey[])

Sorts the rows of a range by one or more of its columns, as Excel's Data > Sort does.

public void Sort(XlsxCellRange range, XlsxSortOptions? options, params XlsxSortKey[] keys)

Parameters

range XlsxCellRange

The cells sorted, the header row included when HasHeader says there is one. Cells outside it do not move.

options XlsxSortOptions

Whether there is a header and whether case counts; null for neither.

keys XlsxSortKey[]

The columns to sort by, the first deciding first.

Remarks

The order is Excel's (measured, tools/Probe-ExcelSorts.ps1): numbers (dates among them) from the smallest, then text, then FALSE before TRUE, then errors, which are all equal. Empty cells come last whichever way the sort goes; descending is otherwise the exact reverse. Rows whose keys are equal keep the order they had. Text is compared as Excel compares it: without regard to case unless asked, an accented letter after the same letter without, digits as characters (so "10" comes before "2", and a number stored as text is text), and hyphens and apostrophes counted only when everything else is equal. A cell holding a formula is sorted by the result the file stores for it.

What moves is each cell of the range with its value or formula, its format, its note and its hyperlink. Row heights, hidden rows, conditional formats and validations stay where they are, as in Excel. A formula that moves is written as Excel writes it: a reference without a sheet name moves with the row, as a copied formula's would, and a reference with one (even to this sheet) or to a defined name does not. Formulas elsewhere that point into the range are not changed.

The sort is recorded in the sheet as Excel records it (sortState). Nothing is calculated: the saved workbook asks for a calculation when it is next opened, and moved formulas have no result until then.

Exceptions

ArgumentNullException

keys or one of them is null.

ArgumentException

No key is given, more than 64 are, a key's column is outside the range, or the range reaches outside the worksheet grid.

InvalidOperationException

The sheet is not a worksheet.

UnsupportedFeatureException

The rows sorted cross a merged range, part of an array formula, a table's header or totals row, or a filter that hides rows.

InvalidDocumentException

A part of the workbook is malformed.