ui: improve sort utility for metrics (#9247)

This improves the UI sorting utility to check for metrics data and sort
after cleaning the string data.

Fixes #8663

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
Rohit Yadav 2024-06-14 12:13:02 +05:30 committed by GitHub
parent 2ca0857bd5
commit 6ce2a58f9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,13 +23,19 @@ function filterNumber (value) {
}
function stringComparator (a, b) {
return a.toString().localeCompare(b.toString())
return a.toString().localeCompare(b.toString(), undefined, { numeric: true })
}
function numericComparator (a, b) {
return filterNumber(a) < filterNumber(b) ? 1 : -1
}
function metricComparator (ma, mb) {
var a = ('' + ma).replace(/((%)|(Ghz)|(Mhz)|(MiB)|(GiB)|(GB)).*$/g, '')
var b = ('' + mb).replace(/((%)|(Ghz)|(Mhz)|(MiB)|(GiB)|(GB)).*$/g, '')
return parseFloat(a) < parseFloat(b) ? 1 : -1
}
function ipV4AddressCIDRComparator (a, b) {
a = a.split(/[./]/gm)
b = b.split(/[./]/gm)
@ -76,6 +82,10 @@ function isNumeric (obj) {
return !Array.isArray(obj) && !isNaN(filterNumber(obj))
}
function isMetric (value) {
return /^[0-9\\. ]*((%)|(Ghz)|(Mhz)|(MiB)|(GiB)|(GB))/.test(value)
}
/**
* Compare elements, attempting to determine type of element to get the best comparison
*
@ -97,6 +107,9 @@ export function genericCompare (a, b) {
if (isNumeric(a) && isNumeric(b)) {
comparator = numericComparator
}
if (isMetric(a) && isMetric(b)) {
comparator = metricComparator
}
return comparator(a, b)
}