sortable.tsx 649 B

1234567891011121314151617181920212223242526272829
  1. // Libraries
  2. import isNumber from 'lodash/isNumber';
  3. import { TableData } from '@grafana/ui';
  4. export function sortTableData(data: TableData, sortIndex?: number, reverse = false): TableData {
  5. if (isNumber(sortIndex)) {
  6. const copy = {
  7. ...data,
  8. rows: data.rows.map((row, index) => {
  9. return row;
  10. }),
  11. };
  12. copy.rows.sort((a, b) => {
  13. a = a[sortIndex];
  14. b = b[sortIndex];
  15. // Sort null or undefined separately from comparable values
  16. return +(a == null) - +(b == null) || +(a > b) || -(a < b);
  17. });
  18. if (reverse) {
  19. copy.rows.reverse();
  20. }
  21. return copy;
  22. }
  23. return data;
  24. }