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

Function sortData

packages/table-core/src/utils/getSortedRowModel.ts:45–109  ·  view source on GitHub ↗
(rows: Row<TData>[])

Source from the content-addressed store, hash-verified

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)
63
64 const aUndefined = aValue === undefined
65 const bUndefined = bValue === undefined
66
67 if (aUndefined || bUndefined) {
68 if (sortUndefined === 'first') return aUndefined ? -1 : 1
69 if (sortUndefined === 'last') return aUndefined ? 1 : -1
70 sortInt =
71 aUndefined && bUndefined
72 ? 0
73 : aUndefined
74 ? sortUndefined
75 : -sortUndefined
76 }
77 }
78
79 if (sortInt === 0) {
80 sortInt = columnInfo.sortingFn(rowA, rowB, sortEntry.id)
81 }
82
83 // If sorting is non-zero, take care of desc and inversion
84 if (sortInt !== 0) {
85 if (isDesc) {
86 sortInt *= -1
87 }
88
89 if (columnInfo.invertSorting) {
90 sortInt *= -1
91 }
92
93 return sortInt
94 }
95 }
96
97 return rowA.index - rowB.index
98 })
99
100 // If there are sub-rows, sort them
101 sortedData.forEach(row => {
102 sortedFlatRows.push(row)

Callers 1

getSortedRowModelFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…