Utils.ts 872 B

123456789101112131415161718192021222324252627282930313233343536
  1. type numberObj = {
  2. [x: string]: number;
  3. };
  4. export const descendingComparator = (
  5. a: numberObj,
  6. b: numberObj,
  7. orderBy: React.ReactText
  8. ) => {
  9. if (b[orderBy] < a[orderBy]) {
  10. return -1;
  11. }
  12. if (b[orderBy] > a[orderBy]) {
  13. return 1;
  14. }
  15. return 0;
  16. };
  17. export const getComparator = (order: string, orderBy: string) => {
  18. return order === 'desc'
  19. ? (a: numberObj, b: numberObj) => descendingComparator(a, b, orderBy)
  20. : (a: numberObj, b: numberObj) => -descendingComparator(a, b, orderBy);
  21. };
  22. export const stableSort = (
  23. array: any[],
  24. comparator: { (a: numberObj, b: numberObj): number }
  25. ) => {
  26. const stabilizedThis = array.map((el, index) => [el, index]);
  27. stabilizedThis.sort((a, b) => {
  28. const order = comparator(a[0], b[0]);
  29. if (order !== 0) return order;
  30. return a[1] - b[1];
  31. });
  32. return stabilizedThis.map(el => el[0]);
  33. };