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

132
Views
Add an Identifier to an object array that doesn't have one using JavaScript

I am new to StackOverflow so I apologize in advance for not being able to type everything structurally.

I have been trying for some time now to add an identifier to my array to use the .sort() function of javascript to have the array content in alphabetical order. It is also important to not disassociate the amount with the name in the array.

My array contains the following when I use the console.log(result);:

['0.4712', 'ARECIBO']
['0.5041', 'CAGUAS']
['0.4121', 'HUMACAO']
['0.4578', 'MAYAGUEZ']
['0.4785', 'PONCE']
['0.4038', 'SAN JUAN']
['0.4318', 'BAYAMON']

I have read many posts and have seen many users post this type of solution...

result.sort(function(a, b) {
            return b[1] - a[0];
        });
        console.log(result);

Sadly, it does not work for me. I strongly believe that not only is my lack of knowledge an impeding factor, but also my lack of identifier in my array. Therefore, I implore your knowledge to solve this issue. If you can provide a brief explanation as to how the solution works, I would be grateful since it helps me grow and understand what I am doing.

Thank you all!

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

0

var a = [
  ['0.4712', 'ARECIBO'],
  ['0.5041', 'CAGUAS'],
  ['0.4121', 'HUMACAO'],
  ['0.4578', 'MAYAGUEZ'],
  ['0.4785', 'PONCE'],
  ['0.4038', 'SAN JUAN'],
  ['0.4318', 'BAYAMON']
]
var result = a.sort(function(a, b) {
  return a[1].localeCompare(b[1]); //compare 2 strings alphbetically
});
console.log(result);

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!