Commit a8c985de by ryan

move sort to table processing

parent 90510b5f
...@@ -11,7 +11,7 @@ import { ...@@ -11,7 +11,7 @@ import {
} from 'react-virtualized'; } from 'react-virtualized';
import { Themeable } from '../../types/theme'; import { Themeable } from '../../types/theme';
import { sortTableData } from '../../utils/processTimeSeries'; import { sortTableData } from '../../utils/processTableData';
import { TableData, InterpolateFunction } from '@grafana/ui'; import { TableData, InterpolateFunction } from '@grafana/ui';
import { import {
......
import { TableData, Column } from '../types/index'; // Libraries
import isNumber from 'lodash/isNumber';
import Papa, { ParseError, ParseMeta } from 'papaparse'; import Papa, { ParseError, ParseMeta } from 'papaparse';
// Types
import { TableData, Column } from '../types';
// Subset of all parse options // Subset of all parse options
export interface TableParseOptions { export interface TableParseOptions {
headerIsFirstLine?: boolean; // Not a papa-parse option headerIsFirstLine?: boolean; // Not a papa-parse option
...@@ -131,3 +134,24 @@ export function parseCSV(text: string, options?: TableParseOptions, details?: Ta ...@@ -131,3 +134,24 @@ export function parseCSV(text: string, options?: TableParseOptions, details?: Ta
columnMap: {}, columnMap: {},
}); });
} }
export function sortTableData(data: TableData, sortIndex?: number, reverse = false): TableData {
if (isNumber(sortIndex)) {
const copy = {
...data,
rows: [...data.rows].sort((a, b) => {
a = a[sortIndex];
b = b[sortIndex];
// Sort null or undefined separately from comparable values
return +(a == null) - +(b == null) || +(a > b) || -(a < b);
}),
};
if (reverse) {
copy.rows.reverse();
}
return copy;
}
return data;
}
...@@ -4,7 +4,7 @@ import isNumber from 'lodash/isNumber'; ...@@ -4,7 +4,7 @@ import isNumber from 'lodash/isNumber';
import { colors } from './colors'; import { colors } from './colors';
// Types // Types
import { TimeSeries, TableData, TimeSeriesVMs, NullValueMode, TimeSeriesValue } from '../types'; import { TimeSeries, TimeSeriesVMs, NullValueMode, TimeSeriesValue } from '../types';
interface Options { interface Options {
timeSeries: TimeSeries[]; timeSeries: TimeSeries[];
...@@ -173,24 +173,3 @@ export function processTimeSeries({ timeSeries, nullValueMode }: Options): TimeS ...@@ -173,24 +173,3 @@ export function processTimeSeries({ timeSeries, nullValueMode }: Options): TimeS
return vmSeries; return vmSeries;
} }
export function sortTableData(data: TableData, sortIndex?: number, reverse = false): TableData {
if (isNumber(sortIndex)) {
const copy = {
...data,
rows: [...data.rows].sort((a, b) => {
a = a[sortIndex];
b = b[sortIndex];
// Sort null or undefined separately from comparable values
return +(a == null) - +(b == null) || +(a > b) || -(a < b);
}),
};
if (reverse) {
copy.rows.reverse();
}
return copy;
}
return data;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment