Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
How to Sort List of Objects by date AND time in React JS or Java script using moment js

i was working with some data that looks like this.

response={data:[{
    "time": "08:00",
    "date": "2022-06-30"
}, {
    "time": "16:30",
    "date": "2022-06-30",
}, {
    "time": "17:00",
    "date": "2022-06-27"
}, {
    "time": "11:00",
    "date": "2022-06-14"
}, {
    
    "time": "17:00",
    "date": "2022-06-14"
}, {
    "time": "08:30",
    "date": "2022-06-02"
}, {
    "time": "16:30",
    "date": "2022-06-02"
}, {
    "time": "16:30",
    "date": "2022-06-02"
}, {
    "time": "16:25",
    "date": "2022-06-02",
}]}

i wanted to sort this data by both of two fields (time & date).

  • by date, it should be in ascending order
  • by time, it should be in ascending order if dates are same in some items

requirement looks quite amazing. i searched on internet but can't get required results.

so i spent some time on it and made my own solution. so i am here to share this solution with stackoverflow-community.

here is my solution.

import moment from 'moment';
import { forEach, get, reverse } from 'lodash';
const data = get(response, 'data', []);
    //as data looks already in descending order by date
    reverse(data);
    //place datetime object in all items
    forEach(data, function(item) {
      item['datetime'] = moment(item.date + 'T' + item.time + ':' + '00');
    });
    //re-sort data by time in ascending order using datetime object
    data.sort(function(a, b) {
      return a.datetime - b.datetime;
    });

it is working finw but kindly let me know if we have any better sollution that should be efficient.

also let me know about your remarks for this solution.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The task can also be done without lodash, simply by comparing date and time strings. The comparison of the dates happens first. If one is larger than the other then the second comparison of the times will not even happen, as the || (or) operator will only evaluate its second argument if the first argument was calculated as "falsy" (in our case 0).

const response={data:[{
"time": "08:00",
"date": "2022-06-30"
}, {
"time": "16:30",
"date": "2022-06-30"
}, {
"time": "17:00",
"date": "2022-06-27"
}, {
"time": "11:00",
"date": "2022-06-14"
}, {
"time": "17:00",
"date": "2022-06-14"
}, {
"time": "08:30",
"date": "2022-06-02"
}, {
"time": "16:30",
"date": "2022-06-02"
}, {
"time": "16:30",
"date": "2022-06-02"
}, {
"time": "16:25",
"date": "2022-06-02"
}]};

const res=response.data.slice(0).sort((a,b)=>
  a.date.localeCompare(b.date)||a.time.localeCompare(b.time));

console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda