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

161
Views
how do I sort object by first element in value list

I have javascript object like this,

var list = {
  "you": [100,'x',67],
  "me": [456,'xxx',68],
  "foo": [7856,'yux',69],
  "bar": [2,'xcv',45]
};

I am trying to sort it according to the first element in the value list. like this

var list = {
    "foo": [7856,'yux',69],
    "me": [456,'xxx',68],
    "you": [100,'x',67],
    "bar": [2,'xcv',45],
};

I couldn't find any resources, with similar implementation in java script.

can anyone help?

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

0

I totally agree with @Andreas about Does JavaScript guarantee object property order? . But maybe an array output is what you expect.

var list = {
  "you": [100,'x',67],
  "me": [456,'xxx',68],
  "foo": [7856,'yux',69],
  "bar": [2,'xcv',45]
};
var arr = [];
for (const [key, value] of Object.entries(list)) {
  arr.push({k:key, v:value})
}
arr = arr.sort((a, b) => b.v[0] - a.v[0])
console.log(arr);

about 4 years ago · Juan Pablo Isaza Report

0

thanks @YuTing for the help.

working solution

var sortable = [];
for (var i in list) {
    sortable.push([i, list[i]])
}

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

// console.log(sortable);
var objSorted = {}
sortable.forEach(function(item){
    objSorted[item[0]]=item[1]
})

console.log(objSorted);
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!