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

174
Views
How to sort an array based on the second value with javascript?

I have an array:

[
    [0, 'Please select a Customer'],
    [2072, 'S-Customer'],
    [834, '01-Customer'],
    [709, 'C-Customer'],
    [4217, 'Test'],
    [2074, 'A-Customer']
]

but how can I sort it numeric and alphabetically and still have the same ID which is the first value?? It would be like this:

obs: the first value should not be changed.

[
    [0, 'Please select a Customer'],
    [834, '01-Customer'],
    [2074, 'A-Customer'],
    [709, 'C-Customer'],
    [2072, 'S-Customer'],
    [4217, 'Test']
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could keep the first array in place by looking to index 0 and sort the rest by the value of index 1

const
    array = [[0, 'Please select a Customer'], [2072, 'S-Customer'], [834, '01-Customer'], [709, 'C-Customer'], [4217, 'Test'], [2074, 'A-Customer']];
    

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

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!