Apologies for the title, this is certainly a bit of a pretentious issue. I've got a range of numbers distributed across two arrays. These arrays hold the same dateset, however one has been converted to strings so that a reference key/tag can be assigned to each value indicating a category index.
const array1 = ["0:0", "0:5.476", "0:11.251", "0:31.916", "0:44.007", "0:53.911", "0:58.628", "0:65.729", "0:75.222", "0:88.849", "0:100", "1:0", "1:44.839", "1:60.303", "1:69.072", "1:73.150", "1:85.800", "1:100", "2:0", "2:11.796", "2:26.525", "2:81.782", "2:88.367", "2:100"]
0:
1:
2:
represent the category index.
The second representation of the dateset is numeric and assigned to a separate array in ascending order.
const array2 = [0, 0, 0, 5.476, 11.251, 11.796, 26.525, 31.916, 44.007, 44.839, 53.911, 58.628, 60.303, 65.729, 69.072, 73.150, 75.222, 81.782, 85.800, 88.367, 88.849, 100, 100, 100]
The goal is to replicate the order of array2
in array1
whilst keeping the category index keys intact. As can be seen in the example below.
> console.log(array1);
["0:0", "1:0", "2:0", "0:5.476", "0:11.251", "2:11.796", "2:26.525", "0:31.916", "0:44.007", "1:44.839", "0:53.911", "0:58.628", "1:60.303", "0:65.729", "1:69.072", "1:73.150", "0:75.222", "2:81.782", "1:85.800", "2:88.367", "0:88.849", "0:100", "1:100", "2:100"]
Of course if I just wanted to match the order I could just un-string array1
and run it in ascending order. This example below un-strings all the 0:
values.
let array1Ascend = [];
for(i=0;i<array1.length;i++){
array1Ascent.push(Number(array1[i].match(/(?!0*:).*/)))
}
array1Ascent.sort(function(a, b) {
return a - b;
});
But the issue is that i'd of course lose the category index assignment of each value in doing that, which defeats the purpose. I'd say i'm more stuck on understanding the logic of this issue if anything. Any help would be appreciated, thank you.