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

172
Views
Ordenar "día", "semana", "mes" y "año"

Tengo una matriz de objetos de una API.

 [ {path: 'image', location: 'LOCATION_1', time: 'day'} 1: {path: 'image', location: 'LOCATION_2', time: 'week'} 2: {path: 'image', location: 'LOCATION_1', time: 'year'} 3: {path: 'image', location: 'LOCATION_1', time: 'week'} 4: {path: 'image', location: 'LOCATION_1', time: 'month'} 5: {path: 'image', location: 'LOCATION_2', time: 'day'} 6: {path: 'image', location: 'LOCATION_2', time: 'month'} 7: {path: 'image', location: 'LOCATION_2', time: 'year'} ]

Quiero ordenarlos según el tiempo, pero cuando lo hago, es como

 day month week year

¿Hay alguna manera de hacerlo como:

 day week month year

Estoy usando un componente funcional en javascript.

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

0

En la función de clasificación, está comparando con la cadena, por eso obtiene una lista ordenada con orden alfabético. Si desea un pedido personalizado, puede crear un mapa para seguir el pedido.

 const data = [ {path: 'image', location: 'LOCATION_1', time: 'day'}, {path: 'image', location: 'LOCATION_2', time: 'week'}, {path: 'image', location: 'LOCATION_1', time: 'year'}, {path: 'image', location: 'LOCATION_1', time: 'week'}, {path: 'image', location: 'LOCATION_1', time: 'month'}, {path: 'image', location: 'LOCATION_2', time: 'day'}, {path: 'image', location: 'LOCATION_2', time: 'month'}, {path: 'image', location: 'LOCATION_2', time: 'year'}, ]; const timeMap = { day: 0, week: 1, month: 2, year: 3, } data.sort((a, b) => { return timeMap[a.time] - timeMap[b.time]; }); console.log(data);

about 4 years ago · Juan Pablo Isaza Report

0

use el método de clasificación como este
mdn: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

 let arr = [ {path: 'image', location: 'LOCATION_1', time: 'day'}, {path: 'image', location: 'LOCATION_2', time: 'week'}, {path: 'image', location: 'LOCATION_1', time: 'year'}, {path: 'image', location: 'LOCATION_1', time: 'week'}, {path: 'image', location: 'LOCATION_1', time: 'month'}, {path: 'image', location: 'LOCATION_2', time: 'day'}, {path: 'image', location: 'LOCATION_2', time: 'month'}, {path: 'image', location: 'LOCATION_2', time: 'year'} ]; let sortBy = ["day", "week", "month", "year"] console.log(arr.sort((a,b) => sortBy.indexOf(a.time) - sortBy.indexOf(b.time)))

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!