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

225
Views
¿La mejor manera de ordenar la matriz en función de diferentes conjuntos de datos?
var array = [{value:"13",type:"Fruit"},{value:"61",type:"Animal"}, {value:"19",type:"Fruit"},{value:"71",type:"Animal"}, {value:"12",type:"Fruit"},{value:"15",type:"Fruit"}, {value:"11",type:"Plant"},{value:"10",type:"Fruit"}, {value:"16",type:"Plant"}]

¿Cuál es la forma mejor u optimizada de ordenar esta matriz de modo que obtenga todos los elementos de tipo Fruit primero y luego los elementos de tipo Animal (seleccionando elementos del final de la matriz)?

 expectedOutput = [{value:"10",type:"Fruit"}, {value:"15",type:"Fruit"},{value:"12",type:"Fruits"}, {value:"19",type:"Fruit"},{value:"13",type:"Fruit"}, {value:"71",type:"Animal"},{value:"61",type:"Animal"}, {value:"16",type:"Plant"},{value:"11",type:"Plant"}]

Nota: - No necesito ordenarlo alfabéticamente. Debe ordenarse según el tipo específico.

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

0

Puede almacenar el orden de las propiedades de type en una matriz y luego restar el índice de la propiedad de type al ordenar para determinar la precedencia:

 const order = ["Fruits", "Animal"] var array = [{value:"13",type:"Fruit"},{value:"61",type:"Animal"}, {value:"19",type:"Fruit"},{value:"71",type:"Animal"}, {value:"12",type:"Fruit"},{value:"15",type:"Fruit"}] const sorted = array.sort((a,b) => order.indexOf(a.type) - order.indexOf(b.type)) console.log(sorted)

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!