Skip to content

Commit

Permalink
[docs] Add notes to Table demo about stableSort (#27025)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosGomez-dev committed Jun 30, 2021
1 parent 9ef6f74 commit a89b3e3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/src/pages/components/tables/EnhancedTable.js
Expand Up @@ -64,6 +64,8 @@ function getComparator(order, orderBy) {
: (a, b) => -descendingComparator(a, b, orderBy);
}

// This method is created for cross-browser compatibility, if you don't
// need to support IE11, you can use Array.prototype.sort() directly
function stableSort(array, comparator) {
const stabilizedThis = array.map((el, index) => [el, index]);
stabilizedThis.sort((a, b) => {
Expand Down Expand Up @@ -299,6 +301,8 @@ export default function EnhancedTable() {
rowCount={rows.length}
/>
<TableBody>
{/* if you don't need to support IE11, you can replace the `stableSort` call with:
rows.slice().sort(getComparator(order, orderBy)) */}
{stableSort(rows, getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row, index) => {
Expand Down
4 changes: 4 additions & 0 deletions docs/src/pages/components/tables/EnhancedTable.tsx
Expand Up @@ -85,6 +85,8 @@ function getComparator<Key extends keyof any>(
: (a, b) => -descendingComparator(a, b, orderBy);
}

// This method is created for cross-browser compatibility, if you don't
// need to support IE11, you can use Array.prototype.sort() directly
function stableSort<T>(array: readonly T[], comparator: (a: T, b: T) => number) {
const stabilizedThis = array.map((el, index) => [el, index] as [T, number]);
stabilizedThis.sort((a, b) => {
Expand Down Expand Up @@ -330,6 +332,8 @@ export default function EnhancedTable() {
rowCount={rows.length}
/>
<TableBody>
{/* if you don't need to support IE11, you can replace the `stableSort` call with:
rows.slice().sort(getComparator(order, orderBy)) */}
{stableSort(rows, getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row, index) => {
Expand Down

0 comments on commit a89b3e3

Please sign in to comment.