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

143
Views
Javascript ordena la matriz por fecha y orden alfabético

Quiero que se ordene por fecha y alfabeto en un orden ¿cómo puedo hacer eso? Creo que el orden alfabético funciona bien, pero la fecha no funciona correctamente. Gracias por las respuestas.

Estructura de datos :

 [{ productId: 21, title: "Huawei P40 Lite ", brand: "Huawei", price: 120, discountPercentage: 10, color: "Black", createdDate: "2021-01-15T01:00:00+03:00", }, { productId: 22, title: "Huawei P40 Lite", brand: "Huawei", price: 1026, discountPercentage: 0, color: "Green", createdDate: "2021-01-16T01:00:00+03:00", }, { productId: 23, title: "Apple iPhone 11", brand: "Apple", price: 1220, discountPercentage: 11, color: "White", createdDate: "2021-01-17T01:00:00+03:00", }, { productId: 24, title: "Apple iPhone 12", brand: "Apple", price: 1420, discountPercentage: 11, color: "White", createdDate: "2021-01-18T01:00:00+03:00", }],

Aquí mi trabajo:

 jsfiddle.net/pazyqb01/

Y probé diferentes soluciones para ordenar la fecha de alguna manera, no pude hacer que funcionara.

La matriz ordenada debe ser como la anterior:

 { productId: 24, title: "Apple iPhone 12", brand: "Apple", price: 1420, discountPercentage: 11, color: "White", createdDate: "2021-01-18T01:00:00+03:00", }, { productId: 23, title: "Apple iPhone 11", brand: "Apple", price: 1220, discountPercentage: 11, color: "White", createdDate: "2021-01-17T01:00:00+03:00", }, { productId: 22, title: "Huawei P40 Lite", brand: "Huawei", price: 1026, discountPercentage: 0, color: "Green", createdDate: "2021-01-16T01:00:00+03:00", }, { productId: 21, title: "Huawei P40 Lite ", brand: "Huawei", price: 120, discountPercentage: 10, color: "Black", createdDate: "2021-01-15T01:00:00+03:00", },
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Por aquí:

simplemente siga la lista de sus criterios de clasificación

 const data = [ { productId: 21, title: 'Huawei P40 Lite ', brand: 'Huawei', price: 120, discountPercentage: 10, color: 'Black', createdDate: '2021-01-15T01:00:00+03:00' } , { productId: 22, title: 'Huawei P40 Lite', brand: 'Huawei', price: 1026, discountPercentage: 0, color: 'Green', createdDate: '2021-01-16T01:00:00+03:00' } , { productId: 23, title: 'Apple iPhone 11', brand: 'Apple', price: 1220, discountPercentage: 11, color: 'White', createdDate: '2021-01-17T01:00:00+03:00' } , { productId: 24, title: 'Apple iPhone 12', brand: 'Apple', price: 1420, discountPercentage: 11, color: 'White', createdDate: '2021-01-18T01:00:00+03:00' } ] const fSort = (a,b) => { let Dx = new Date(b.createdDate) - new Date(a.createdDate) // 1st criteria if (Dx===0) Dx = a.title.trim().localeCompare(b.title.trim()) // 2nd // if (Dx===0) Dx = ... // 3rd // if (Dx===0) Dx = ... // 4th.... return Dx } console.log( data.sort(fSort))

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!