Skip to content

Commit

Permalink
[Table] Improved sorting in table for demo EnhancedTable (mui#12736)
Browse files Browse the repository at this point in the history
* improved table sorting logic

* improved table sorting logic
  • Loading branch information
adeelibr authored and oliviertassinari committed Aug 31, 2018
1 parent 2d41601 commit f9632f8
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions docs/src/pages/demos/tables/EnhancedTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ function desc(a, b, orderBy) {
return 0;
}

function stableSort(array, cmp) {
const stabilizedThis = array.map((el, index) => [el, index]);
stabilizedThis.sort((a, b) => {
const order = cmp(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);
}
Expand Down Expand Up @@ -282,8 +292,7 @@ class EnhancedTable extends React.Component {
rowCount={data.length}
/>
<TableBody>
{data
.sort(getSorting(order, orderBy))
{stableSort(data, getSorting(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map(n => {
const isSelected = this.isSelected(n.id);
Expand Down

0 comments on commit f9632f8

Please sign in to comment.