diff options
| author | Bobby <[email protected]> | 2022-01-28 17:06:57 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-01-28 17:06:57 -0500 |
| commit | 3e84c49170fc99a692ce8874d7f1545747919986 (patch) | |
| tree | e228577000b04fa582ed03c872ff4cefa59ba6e1 /src/lib/data.ts | |
| parent | 5aeab5e6a0e71c6a3e8ddfb02c481dac731148de (diff) | |
| download | izuku.js-3e84c49170fc99a692ce8874d7f1545747919986.tar.xz izuku.js-3e84c49170fc99a692ce8874d7f1545747919986.zip | |
feat: sort by column function
Diffstat (limited to 'src/lib/data.ts')
| -rw-r--r-- | src/lib/data.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/lib/data.ts b/src/lib/data.ts index 6a2fe34..e3c1b7d 100644 --- a/src/lib/data.ts +++ b/src/lib/data.ts @@ -193,3 +193,33 @@ export function searchValue( throw new Error('Invalid options'); } } + +/** + * sort - sorts the rowdata based on the column index + * @param column: the column index or column name to be sorted + * @param order: the order of the sort + * @returns the sorted rowdata + */ +export function sort( + this: Frame, + column: number | string, + ord?: 'accending' | 'descending' +) { + const order = ord ? ord : 'ascending'; + const colums = this.columns; + const rowdata = this.rowdata; + const columnIndex = + typeof column === 'number' ? column : colums.indexOf(column); + if (columnIndex === -1) { + throw new Error('Invalid column index'); + } + const sorted = rowdata.sort((a: any, b: any) => { + if (order === 'ascending') { + return a[columnIndex] > b[columnIndex] ? 1 : -1; + } else { + return a[columnIndex] > b[columnIndex] ? -1 : 1; + } + }); + this.rowdata = sorted; + return this; +} |
