mirror of https://github.com/grafana/grafana
parent
2db4004d62
commit
1609c07fb2
@ -1,41 +1,29 @@ |
||||
// Libraries
|
||||
import _ from 'lodash'; |
||||
import isNumber from 'lodash/isNumber'; |
||||
|
||||
import { TableData } from '@grafana/ui'; |
||||
|
||||
export class SortedTableData { |
||||
rows: any[]; |
||||
|
||||
constructor(private data: TableData, sortIndex?: number, reverse?: boolean) { |
||||
if (_.isNumber(sortIndex)) { |
||||
// Make a copy of all the rows
|
||||
this.rows = this.data.rows.map((row, index) => { |
||||
export function sortTableData(data: TableData, sortIndex?: number, reverse = false): TableData { |
||||
if (isNumber(sortIndex)) { |
||||
const copy = { |
||||
...data, |
||||
rows: data.rows.map((row, index) => { |
||||
return row; |
||||
}); |
||||
this.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) { |
||||
this.rows.reverse(); |
||||
} |
||||
} else { |
||||
this.rows = data.rows; |
||||
} |
||||
} |
||||
copy.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); |
||||
}); |
||||
|
||||
getInfo(): any[] { |
||||
return this.data.columns; |
||||
} |
||||
|
||||
getRow(index: number): any[] { |
||||
return this.rows[index]; |
||||
} |
||||
if (reverse) { |
||||
copy.rows.reverse(); |
||||
} |
||||
|
||||
getCount(): number { |
||||
return this.rows.length; |
||||
return copy; |
||||
} |
||||
return data; |
||||
} |
||||
|
Loading…
Reference in new issue