TExcelFile.Sort Method
Overloads
- TExcelFile.Sort(TXlsCellRange, Boolean, Int32Array, TSortOrderArray, TComparer<TCellValue>)
- TExcelFile.Sort(TXlsCellRange, Boolean, Int32Array, TSortOrderArray, TComparer<TCellValue>, TSortFormulaMode)
TExcelFile.Sort(TXlsCellRange, Boolean, Int32Array, TSortOrderArray, TComparer<TCellValue>)
Sorts a range on the current sheet.
Syntax
Unit: FlexCel.Core
procedure TExcelFile.Sort(const Range: TXlsCellRange; const ByRows: Boolean; const Keys: TArray<Int32>; const SortOrder: TArray<TSortOrder>; const Comparer: TComparer<TCellValue>); overload;
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
const | Range | TXlsCellRange | Range to sort. It must not include headers. |
const | ByRows | Boolean | If true, rows will be sorted. If false, columns will. |
const | Keys | TArray<Int32> | An array of integers indicating the columns or rows you want to use for sorting. Note that this number is absolute, for example column 1 always means column "A" no matter if the range we are sorting begins at column "B". A null array means sort by the first column or row, then by the second, etc. up to 8 entries. |
const | SortOrder | TArray<TSortOrder> | An array of flags indicating whether to sort ascending or descending for each Key. If null, all sorts will be ascending. If not null and the array size is less than the size of the "Keys" parameter, all missing entries are assumed to be Ascending. |
const | Comparer | TComparer<TCellValue> | Comparer to create a custom way to compare the different items. Set it to null to use default ordering. |
Examples
To sort the range of cells from B7 to D100, on columns B Ascending and C Descending:
xls.Sort(
TXlsCellRange.Create('B7:D100'), //To sort the full sheet, you can pass TXlsCellRange.Null here.
true, //Normally we want to sort the rows in the range. But we might want to sort columns and we can do it by setting this to false.
Int32Array.Create(2, 3), // Sort by columns B and C (2 and 3)
TArray<TSortOrder>.Create(TSortOrder.Ascending, TSortOrder.Descending), //Sort column B ascending, column C descending. If you want to sort everything in ascending order, just set this to nil.
nil // Use the standard comparer.
);
See also
TExcelFile.Sort(TXlsCellRange, Boolean, Int32Array, TSortOrderArray, TComparer<TCellValue>, TSortFormulaMode)
Sorts a range on the current sheet.
Syntax
Unit: FlexCel.Core
procedure TExcelFile.Sort(const Range: TXlsCellRange; const ByRows: Boolean; const Keys: TArray<Int32>; const SortOrder: TArray<TSortOrder>; const Comparer: TComparer<TCellValue>; const SortFormulaMode: TSortFormulaMode); overload; virtual; abstract;
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
const | Range | TXlsCellRange | Range to sort. It must not include headers. |
const | ByRows | Boolean | If true, rows will be sorted. If false, columns will. |
const | Keys | TArray<Int32> | An array of integers indicating the columns or rows you want to use for sorting. Note that this number is absolute, for example column 1 always means column "A" no matter if the range we are sorting begins at column "B". A null array means sort by the first column or row, then by the second, etc. up to 8 entries. |
const | SortOrder | TArray<TSortOrder> | An array of flags indicating whether to sort ascending or descending for each Key. If null, all sorts will be ascending. If not null and the array size is less than the size of the "Keys" parameter, all missing entries are assumed to be Ascending. |
const | Comparer | TComparer<TCellValue> | Comparer to create a custom way to compare the different items. Set it to null to use default ordering. |
const | SortFormulaMode | TSortFormulaMode | Defines how formulas in the file will be changed when sorting. |
Examples
To sort the range of cells from B7 to D100, on columns B Ascending and C Descending using TSortFormulaMode.ExcelLike:
xls.Sort(
TXlsCellRange.Create('B7:D100'), //To sort the full sheet, you can pass TXlsCellRange.Null here.
true, //Normally we want to sort the rows in the range. But we might want to sort columns and we can do it by setting this to false.
Int32Array.Create(2, 3), // Sort by columns B and C (2 and 3)
TArray<TSortOrder>.Create(TSortOrder.Ascending, TSortOrder.Descending), //Sort column B ascending, column C descending. If you want to sort everything in ascending order, just set this to nil.
nil, // Use the standard comparer.
TSortFormulaMode.ExcelLike //Excel-like is faster, but won't change formulas outside the range being sorted.
);