Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
How to sort by Numbers first then Alphabetical

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!