I’m trying to sort an array:
["B3", "D2", "F1", "A9", "D12", "A2", "C1", "Z0", "B1"]
The expected output should be:
["Z0", "B1", "C1", "F1", "A2", "D2", "B3", "A9", "D12"]
Here’s my code:
let array = ["B3", "D2", "F1", "A9", "D12", "A2", "C1", "Z0", "B1"];
let collator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: "base",
});
console.log(array.sort(collator.compare));
The output is then sorted by Alphabet first, which gives:
["A2", "A9", "B1", "B3", "C1", "D2", "D12", "F1", "Z0"]
So I figured out that if I switch the position of ever value like this:
["3B", "2D", "1F", "9A", "12D", "2A", "1C", "0Z", "1B"]
And then pass it in collator again, it’ll give me the correct sorted sequence, but just that the number and letters are flipped. I’ll then have to flip them back. Is there a better way of doing this?