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

97
Views
Ordenar 2d Array por orden ascendente usando Javascript

Probé un mapa y un bucle forEach y todavía no puedo ordenarlo por orden alfabético. No estoy seguro de lo que me estoy perdiendo aquí.

Mi matriz:

 const filtData = [ [ { gameType: "Rowing", }, { gameType: "Rowing", }, { gameType: "Rowing", }, ], [ { gameType: "Golf", }, { gameType: "Golf", }, { gameType: "Golf", }, ], [ { gameType: "Soccer", }, { gameType: "Soccer", }, { gameType: "Soccer", }, ], [ { gameType: "Baseball", }, { gameType: "Baseball", }, { gameType: "Baseball", }, ], ]

js:

 filtData.forEach(d => d.sort((a,b) => a.gameType - b.gameType)) console.log(filtData) /* <--fails */ const sortedData = filtData.map((d) => d.sort((a, b) => { return a.gameType - b.gameType; })); console.log("sortedData", sortedData)/* <--fails */

JsFiddle: https://jsfiddle.net/eL510oq3/

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

0

Algunos puntos:

  • Está ordenando según cadenas, no números, así que use String#localeCompare
  • No es necesario usar forEach o .map , solo sort

 const filtData = [ [ { gameType: "Rowing", }, { gameType: "Rowing", }, { gameType: "Rowing", }, ], [ { gameType: "Golf", }, { gameType: "Golf", }, { gameType: "Golf", }, ], [ { gameType: "Soccer", }, { gameType: "Soccer", }, { gameType: "Soccer", }, ], [ { gameType: "Baseball", }, { gameType: "Baseball", }, { gameType: "Baseball", }, ], ]; filtData.sort((a,b) => a[0].gameType.localeCompare(b[0].gameType)) console.log(filtData);

about 4 years ago · Juan Pablo Isaza Report

0

Clasificación de objetos de matriz en el método de orden ASC 1:

 let arrayConcat = filtData.reduce((preValue, curValue) => { return preValue.concat(curValue) }, []); let result = arrayConcat.sort((a, b) => { return a.gameType.localeCompare(b.gameType); }) 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!