sortable.tsx 597 B

12345678910111213141516171819202122232425
  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].sort((a, b) => {
  9. a = a[sortIndex];
  10. b = b[sortIndex];
  11. // Sort null or undefined separately from comparable values
  12. return +(a == null) - +(b == null) || +(a > b) || -(a < b);
  13. }),
  14. };
  15. if (reverse) {
  16. copy.rows.reverse();
  17. }
  18. return copy;
  19. }
  20. return data;
  21. }