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

146
Views
javascript ordenando una matriz json por una clave dada

dado

 let jsons = [ { "id": 10 ... }, { "id": 4 ... }, { "id": 1 ... }, ... ]

Quiero que los jsons se ordenen por id, por ejemplo.

 let jsons = [ { "id": 1 ... }, { "id": 2 ... }, { "id": 3 ... }, ... ]

Esto es lo que he hecho para hacer esto (pero es extremadamente ineficiente)

 let jsons = [ { "id": 10 }, { "id": 4 }, { "id": 1 }, ] let new_list = [] length = jsons.length for(let i = 0; i < length; i++) { let index = 0 let cur_lowest = 50000 // not sure how to get an infinite upper_bound for(let j = 0; j < jsons.length; j++) { if(jsons[j].id <= cur_lowest) { cur_lowest = jsons[j].id index = j } } new_list.push(jsons[index]) jsons.splice(index, 1) } console.log(new_list);

Entonces esto funciona, lo hice aquí https://jsfiddle.net/xceybhLg/5/

¿Pero hay una manera más simple? Preferiblemente con la función ordenada, creo que se puede adjuntar a las teclas, pero al leer la documentación, realmente no lo entiendo.

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

0

¿Por qué no puede usar una función de clasificación simple que acepte una lista y la clave contra la cual se realizará la acción de clasificación?

 let jsons = [ { "id": 10 }, { "id": 4 }, { "id": 1 }, ]; function sortList(list, key) { return list.sort((a, b) => a[key] - b[key]); } const new_list = sortList(jsons, 'id'); console.log(new_list);

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!