sortable.tsx 886 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Libraries
  2. import _ from 'lodash';
  3. import { TableData } from '@grafana/ui';
  4. export class SortedTableData {
  5. rows: any[];
  6. constructor(private data: TableData, sortIndex?: number, reverse?: boolean) {
  7. if (_.isNumber(sortIndex)) {
  8. // Make a copy of all the rows
  9. this.rows = this.data.rows.map((row, index) => {
  10. return row;
  11. });
  12. this.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. this.rows.reverse();
  20. }
  21. } else {
  22. this.rows = data.rows;
  23. }
  24. }
  25. getInfo(): any[] {
  26. return this.data.columns;
  27. }
  28. getRow(index: number): any[] {
  29. return this.rows[index];
  30. }
  31. getCount(): number {
  32. return this.rows.length;
  33. }
  34. }