diff --git a/docs/src/pages/components/tables/EnhancedTable.js b/docs/src/pages/components/tables/EnhancedTable.js index b50e12a03365b5..e923ccbf2afcf4 100644 --- a/docs/src/pages/components/tables/EnhancedTable.js +++ b/docs/src/pages/components/tables/EnhancedTable.js @@ -41,7 +41,7 @@ const rows = [ createData('Oreo', 437, 18.0, 63, 4.0), ]; -function desc(a, b, orderBy) { +function descendingComparator(a, b, orderBy) { if (b[orderBy] < a[orderBy]) { return -1; } @@ -51,20 +51,22 @@ function desc(a, b, orderBy) { return 0; } -function stableSort(array, cmp) { +function getComparator(order, orderBy) { + return order === 'desc' + ? (a, b) => descendingComparator(a, b, orderBy) + : (a, b) => -descendingComparator(a, b, orderBy); +} + +function stableSort(array, comparator) { const stabilizedThis = array.map((el, index) => [el, index]); stabilizedThis.sort((a, b) => { - const order = cmp(a[0], b[0]); + const order = comparator(a[0], b[0]); if (order !== 0) return order; return a[1] - b[1]; }); return stabilizedThis.map(el => el[0]); } -function getSorting(order, orderBy) { - return order === 'desc' ? (a, b) => desc(a, b, orderBy) : (a, b) => -desc(a, b, orderBy); -} - const headCells = [ { id: 'name', numeric: false, disablePadding: true, label: 'Dessert (100g serving)' }, { id: 'calories', numeric: true, disablePadding: false, label: 'Calories' }, @@ -293,7 +295,7 @@ export default function EnhancedTable() { rowCount={rows.length} /> - {stableSort(rows, getSorting(order, orderBy)) + {stableSort(rows, getComparator(order, orderBy)) .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) .map((row, index) => { const isItemSelected = isSelected(row.name); diff --git a/docs/src/pages/components/tables/EnhancedTable.tsx b/docs/src/pages/components/tables/EnhancedTable.tsx index 377bc92c56d77e..8a62992e29e4f5 100644 --- a/docs/src/pages/components/tables/EnhancedTable.tsx +++ b/docs/src/pages/components/tables/EnhancedTable.tsx @@ -54,7 +54,7 @@ const rows = [ createData('Oreo', 437, 18.0, 63, 4.0), ]; -function desc(a: T, b: T, orderBy: keyof T) { +function descendingComparator(a: T, b: T, orderBy: keyof T) { if (b[orderBy] < a[orderBy]) { return -1; } @@ -64,25 +64,27 @@ function desc(a: T, b: T, orderBy: keyof T) { return 0; } -function stableSort(array: T[], cmp: (a: T, b: T) => number) { +type Order = 'asc' | 'desc'; + +function getComparator( + order: Order, + orderBy: Key, +): (a: { [key in Key]: number | string }, b: { [key in Key]: number | string }) => number { + return order === 'desc' + ? (a, b) => descendingComparator(a, b, orderBy) + : (a, b) => -descendingComparator(a, b, orderBy); +} + +function stableSort(array: T[], comparator: (a: T, b: T) => number) { const stabilizedThis = array.map((el, index) => [el, index] as [T, number]); stabilizedThis.sort((a, b) => { - const order = cmp(a[0], b[0]); + const order = comparator(a[0], b[0]); if (order !== 0) return order; return a[1] - b[1]; }); return stabilizedThis.map(el => el[0]); } -type Order = 'asc' | 'desc'; - -function getSorting( - order: Order, - orderBy: K, -): (a: { [key in K]: number | string }, b: { [key in K]: number | string }) => number { - return order === 'desc' ? (a, b) => desc(a, b, orderBy) : (a, b) => -desc(a, b, orderBy); -} - interface HeadCell { disablePadding: boolean; id: keyof Data; @@ -321,7 +323,7 @@ export default function EnhancedTable() { rowCount={rows.length} /> - {stableSort(rows, getSorting(order, orderBy)) + {stableSort(rows, getComparator(order, orderBy)) .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) .map((row, index) => { const isItemSelected = isSelected(row.name);