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

250
Views
¿Cómo ordenar las notificaciones por fechas en reaccionar?

Estoy creando un componente de notificaciones en el que quiero ordenar las notificaciones por fechas y mostrarlas.

Lo intenté pero no funcionó:

 const result = notifications.notificationRows.sort((a, b) => b.time - a.time)

¿Cómo puedo hacerlo sobre una siguiente matriz?

ingrese la descripción de la imagen aquí

 notificationRows: Array(25) 0: notificationRow: fee: "20 TEST" id: "1.11.191431" info: "[userlink=demo3], send 0.00021 TEST to ,[userlink=demo1]" key: "1.11.191431" time: "2022-05-17 16:54:21" type: "transfer" [[Prototype]]: Object unread: false [[Prototype]]: Object 1: notificationRow: fee: "20 TEST" id: "1.11.191430" info: "[userlink=demo3], send 0.012 TEST to ,[userlink=demo1]" key: "1.11.191430" time: "2022-05-17 14:52:39" type: "transfer" [[Prototype]]: Object unread: false [[Prototype]]: Object 2: notificationRow: fee: "20 TEST" id: "1.11.191427" info: "[userlink=demo3], send 0.0021 TEST to ,[userlink=demo1]" key: "1.11.191427" time: "2022-05-17 14:34:15" type: "transfer" [[Prototype]]: Object unread: false

Mi resultado esperado es:

ingrese la descripción de la imagen aquí

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

0

Esto no funciona porque está comparando cadenas en lugar de objetos de fechas. cuando intenta restar las dos fechas en su función de clasificación, el resultado siempre será NaN .

 "2022-02-01 12:00:00" - "2022-02-02 12:00:00" // NaN

Puede usar el objeto Fecha para comparar y ordenar su objeto.

Esto transformará su cadena en un objeto Date que se puede comparar como lo hizo.

 new Date("2022-02-01 12:00:00") - new Date("2022-02-02 12:00:00") // -86400000 meaning the second date is after the first one 

 const notifications = { notificationRows: [{time: '2022-02-01 12:00:00'}, {time: '2022-01-01 12:00:00'}, {time: '2022-03-01 12:00:00'}] } const oldResult = notifications.notificationRows.sort((a, b) => b.time - a.time) console.log("Your result", oldResult) const result = notifications.notificationRows.sort((a, b) => new Date(b.time) - new Date(a.time)) console.log("New result", 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!