2

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?

1 Answers1

4

If you have always a single letter, you could sort by the numerical rest and by the first letter.

const
    array = ["B3", "D2", "F1", "A9", "D12", "A2", "C1", "Z0", "B1"];

array.sort((a, b) => a.slice(1) - b.slice(1) || a[0].localeCompare(b[0]));

console.log(...array);

If you have more than one letter, you could take a regular expression and get only digits and non digits, separated into an object.

const
    getValues = string => ({
        letters: string.match(/\D+/)[0],
        digits: string.match(/\d+/)[0]
    }),
    array = ["B3", "D2", "F1", "A9", "D12", "A2", "C1", "Z0", "B1"];

array.sort((a, b) => {
    const [aa, bb] = [a, b].map(getValues);

    return aa.digits - bb.digits
        || aa.letters.localeCompare(bb.letters);
});

console.log(...array);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • How about `slice(-1)` for >=1 letters – Luca Kiebel Feb 15 '22 at 09:00
  • 1
    @LucaKiebel, it does not work, because you get only a single character from the end. it would not work for `'D12'`. – Nina Scholz Feb 15 '22 at 09:02
  • This is really nice, It's definitely usable if the letters are always going to be single! I checked that if you have multiple letters, Collator will take that into account. So for example if i have "1BB" and "1BA", it'll sort it correctly. But that is if i flip the letters and numbers. – SpookyBoogy Feb 15 '22 at 09:11