( rows: DbRow[], sorts: SortRule[] | undefined, fieldsById: Map<string, DbField> )
| 108 | } |
| 109 | |
| 110 | export function sortRows( |
| 111 | rows: DbRow[], |
| 112 | sorts: SortRule[] | undefined, |
| 113 | fieldsById: Map<string, DbField> |
| 114 | ): DbRow[] { |
| 115 | if (!sorts || sorts.length === 0) return rows |
| 116 | const decorated = rows.map((row, index) => ({ row, index })) |
| 117 | decorated.sort((a, b) => { |
| 118 | for (const sort of sorts) { |
| 119 | const field = fieldsById.get(sort.fieldId) |
| 120 | if (!field) continue |
| 121 | const cmp = compareByField(a.row, b.row, field) |
| 122 | if (cmp !== 0) return sort.direction === 'desc' ? -cmp : cmp |
| 123 | } |
| 124 | return a.index - b.index // stable tie-break |
| 125 | }) |
| 126 | return decorated.map((d) => d.row) |
| 127 | } |
| 128 | |
| 129 | // --------------------------------------------------------------------------- |
| 130 | // Grouping (Board view) |
no test coverage detected