Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1.  
  2. function desc(a, b, orderBy) {
  3. if (b[orderBy] < a[orderBy]) {
  4. return -1;
  5. }
  6. if (b[orderBy] > a[orderBy]) {
  7. return 1;
  8. }
  9. return 0;
  10. }
  11.  
  12. function stableSort(array, cmp) {
  13. const stabilizedThis = array.map((el, index) => [el, index]);
  14. stabilizedThis.sort((a, b) => {
  15. const order = cmp(a[0], b[0]);
  16. if (order !== 0) return order;
  17. return a[1] - b[1];
  18. });
  19. return stabilizedThis.map(el => el[0]);
  20. }
  21.  
  22. function getSorting(order, orderBy) {
  23. return order === 'desc' ? (a, b) => desc(a, b, orderBy) : (a, b) => -desc(a, b, orderBy);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement