Take units into account when sorting

This commit is contained in:
RunasSudo 2023-01-23 20:53:59 +11:00
parent 562ed39600
commit e57518e37b
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
1 changed files with 18 additions and 3 deletions

View File

@ -174,7 +174,7 @@
return 1;
}
// Compare mpp_preferred_term word-by-word accounting for numbers
// Compare mpp_preferred_term word-by-word accounting for numbers and units
const bits1 = item1['mpp_preferred_term'].split(' ');
const bits2 = item2['mpp_preferred_term'].split(' ');
@ -182,8 +182,23 @@
for (let i = 0; i < bits1.length && i < bits2.length; i++) {
if (regexIsNumber.test(bits1[i]) && regexIsNumber.test(bits2[i])) {
// Numeric compare
const num1 = parseInt(bits1[i]);
const num2 = parseInt(bits2[i]);
let num1 = parseInt(bits1[i]);
let num2 = parseInt(bits2[i]);
// Check for units - convert to grams
if (bits1.length > i + 1 && bits1[i + 1] == 'microgram') {
num1 /= 1000000;
}
if (bits1.length > i + 1 && bits1[i + 1] == 'mg') {
num1 /= 1000;
}
if (bits2.length > i + 1 && bits2[i + 1] == 'microgram') {
num2 /= 1000000;
}
if (bits2.length > i + 1 && bits2[i + 1] == 'mg') {
num2 /= 1000;
}
if (num1 < num2) {
return -1;