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

171
Views
Cómo ordenar una matriz de objetos javascript, según una clave

¿Cómo puedo ordenar esta matriz en función de la identificación?

 const arr = [{ "id": 38938888, "subInternalUpdates": true, }, { "id": 38938887, "subInternalUpdates": true }, { "id": 38938889, "subInternalUpdates": true } ]; const sorted_by_name = arr.sort((a, b) => a.id > b.id); console.log(sorted_by_name);

Rendimiento esperado

 const arr = [ { "id": 38938887, "subInternalUpdates": true }, { "id": 38938888, "subInternalUpdates": true, }, { "id": 38938889, "subInternalUpdates": true } ];
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede comparar directamente usando ab ; de lo contrario, si está comparando los valores, debe devolver -1, 0 o 1 para que la ordenación funcione correctamente

 const arr = [{ "id": 38938888, "subInternalUpdates": true, }, { "id": 38938887, "subInternalUpdates": true }, { "id": 38938889, "subInternalUpdates": true } ]; const sorted_by_name = arr.sort((a, b) => a.id - b.id); console.log(sorted_by_name);

about 4 years ago · Juan Pablo Isaza Report

0

Mucho mejor devuelva a.id - b.id cuando ordene la matriz:

 const arr = [{ "id": 38938888, "subInternalUpdates": true, }, { "id": 38938887, "subInternalUpdates": true }, { "id": 38938889, "subInternalUpdates": true } ]; const sorted_by_name = arr.sort((a, b) => { return a.id - b.id; }); console.log(sorted_by_name);

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!