Skip to content

Commit 6ce2a58

Browse files
ui: improve sort utility for metrics (apache#9247)
This improves the UI sorting utility to check for metrics data and sort after cleaning the string data. Fixes apache#8663 Signed-off-by: Rohit Yadav <[email protected]>
1 parent 2ca0857 commit 6ce2a58

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

ui/src/utils/sort.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ function filterNumber (value) {
2323
}
2424

2525
function stringComparator (a, b) {
26-
return a.toString().localeCompare(b.toString())
26+
return a.toString().localeCompare(b.toString(), undefined, { numeric: true })
2727
}
2828

2929
function numericComparator (a, b) {
3030
return filterNumber(a) < filterNumber(b) ? 1 : -1
3131
}
3232

33+
function metricComparator (ma, mb) {
34+
var a = ('' + ma).replace(/((%)|(Ghz)|(Mhz)|(MiB)|(GiB)|(GB)).*$/g, '')
35+
var b = ('' + mb).replace(/((%)|(Ghz)|(Mhz)|(MiB)|(GiB)|(GB)).*$/g, '')
36+
return parseFloat(a) < parseFloat(b) ? 1 : -1
37+
}
38+
3339
function ipV4AddressCIDRComparator (a, b) {
3440
a = a.split(/[./]/gm)
3541
b = b.split(/[./]/gm)
@@ -76,6 +82,10 @@ function isNumeric (obj) {
7682
return !Array.isArray(obj) && !isNaN(filterNumber(obj))
7783
}
7884

85+
function isMetric (value) {
86+
return /^[0-9\\. ]*((%)|(Ghz)|(Mhz)|(MiB)|(GiB)|(GB))/.test(value)
87+
}
88+
7989
/**
8090
* Compare elements, attempting to determine type of element to get the best comparison
8191
*
@@ -97,6 +107,9 @@ export function genericCompare (a, b) {
97107
if (isNumeric(a) && isNumeric(b)) {
98108
comparator = numericComparator
99109
}
110+
if (isMetric(a) && isMetric(b)) {
111+
comparator = metricComparator
112+
}
100113

101114
return comparator(a, b)
102115
}

0 commit comments

Comments
 (0)