MCPcopy
hub / github.com/TanStack/table / getSortedRowModel

Function getSortedRowModel

packages/table-core/src/utils/getSortedRowModel.ts:5–121  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3import { getMemoOptions, memo } from '../utils'
4
5export function getSortedRowModel<TData extends RowData>(): (
6 table: Table<TData>
7) => () => RowModel<TData> {
8 return table =>
9 memo(
10 () => [table.getState().sorting, table.getPreSortedRowModel()],
11 (sorting, rowModel) => {
12 if (!rowModel.rows.length || !sorting?.length) {
13 return rowModel
14 }
15
16 const sortingState = table.getState().sorting
17
18 const sortedFlatRows: Row<TData>[] = []
19
20 // Filter out sortings that correspond to non existing columns
21 const availableSorting = sortingState.filter(sort =>
22 table.getColumn(sort.id)?.getCanSort()
23 )
24
25 const columnInfoById: Record<
26 string,
27 {
28 sortUndefined?: false | -1 | 1 | 'first' | 'last'
29 invertSorting?: boolean
30 sortingFn: SortingFn<TData>
31 }
32 > = {}
33
34 availableSorting.forEach(sortEntry => {
35 const column = table.getColumn(sortEntry.id)
36 if (!column) return
37
38 columnInfoById[sortEntry.id] = {
39 sortUndefined: column.columnDef.sortUndefined,
40 invertSorting: column.columnDef.invertSorting,
41 sortingFn: column.getSortingFn(),
42 }
43 })
44
45 const sortData = (rows: Row<TData>[]) => {
46 // This will also perform a stable sorting using the row index
47 // if needed.
48 const sortedData = rows.map(row => ({ ...row }))
49
50 sortedData.sort((rowA, rowB) => {
51 for (let i = 0; i < availableSorting.length; i += 1) {
52 const sortEntry = availableSorting[i]!
53 const columnInfo = columnInfoById[sortEntry.id]!
54 const sortUndefined = columnInfo.sortUndefined
55 const isDesc = sortEntry?.desc ?? false
56
57 let sortInt = 0
58
59 // All sorting ints should always return in ascending order
60 if (sortUndefined) {
61 const aValue = rowA.getValue(sortEntry.id)
62 const bValue = rowB.getValue(sortEntry.id)

Callers 15

main.tsFile · 0.90
main.tsFile · 0.90
AppFunction · 0.85
AppFunction · 0.85
AppFunction · 0.85
MyTableFunction · 0.85
AppFunction · 0.85
AppFunction · 0.85
AppFunction · 0.85
AppFunction · 0.85
AppFunction · 0.85
AppFunction · 0.85

Calls 3

memoFunction · 0.90
getMemoOptionsFunction · 0.90
sortDataFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…