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

112
Vistas
Sort array by current date

I am looking to sort the values in an array based on the current date.

My values :

[{
  date: "12-28",
  id: 1
}, {
  date: "11-30",
  id: 2
}, {
  date: "09-30",
  id: 3
}, {
  date: "05-30",
  id: 4
}]

I tried to do something like this, but it doesn't really work :

var array = [{id: 1, date:'12-28'}, 
{id: 2, date:'11-30'}, 
{id: 3, date:'09-30'}, 
{id: 4, date:'05-30'}];

const now = Date.now();

array.sort(function(a, b) {     
    var c = new Date(a.date);
    var d = new Date(b.date);
    return (now-c)-(now-d);
});

console.log(array);

If it's November 29th :

[{
  date: "11-30",
  id: 2
}, {
  date: "12-28",
  id: 1
}, {
  date: "05-30",
  id: 4
}, {
  date: "09-30",
  id: 3
}]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

How about turning mm-dd to mmdd and then converting to number and then using Math.abs() as follows?

const arr = [
    {date: "12-28",id: 1}, 
    {date: "11-30",id: 2}, 
    {date: "09-30",id: 3}, 
    {date: "05-30",id: 4}
];

const dt = new Date();
const now = +[dt.getMonth()+1, dt.getDate()].map(v => `0${v}`.slice(-2)).join('');

const ordered = arr.sort((a,b) => Math.abs(+a.date.replace(/[^\d]+/,'') - now) - Math.abs(+b.date.replace(/[^\d]+/,'') - now));

console.log( ordered );

All Dates Future

If all dates are to be considered to be present or future, here is a concept we can employ:

const arr = [
    {date: "12-28",id: 1}, 
    {date: "11-30",id: 2}, 
    {date: "09-30",id: 3}, 
    {date: "05-30",id: 4}
];

//converts m-d to mm-dd
const fmt = md => md.split('-').map(p => `0${p}`.slice(-2)).join('-');
const now = new Date();
const year = new Date().getFullYear();
const nww = fmt(`${now.getMonth()+1}-${now.getDate()}`);
const nw = new Date(`${year}-${nww}`).getTime();

const ordered = arr.map(({
    date: d,
    id
}) => ({
    date: d,
    id,
    d: `${d < nww ? (year + 1) : year}-${d}`
})).map(({
    d,
    ...rest
}) => ({
    ...rest,
    d,
    ...{
        t: new Date(d).getTime()
    }
})).sort((a, b) => (a.t - nw) - (b.t - nw)).map(({
    date,
    id
}) => ({
    date,
    id
}));

console.log( ordered );

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Array.sort() to give you the desired result, I've moved the dates in the input array to this year:

let input = [{ date: "2021-12-28", id: 1 }, { date: "2021-11-30", id: 2 }, { date: "2021-09-30", id: 3 }, { date: "2021-05-30", id: 4 }];

function sortByNearest({ date: a}, {date: b}) {
    const now = Date.now();
    return (Math.abs(Date.parse(a) - now)) - (Math.abs(Date.parse(b) - now));
}

console.log(input.sort(sortByNearest))
    
.as-console-wrapper { max-height: 100% !important; top: 0; }

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