Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
JavaScript sorting after concat of two arrays now working

I've used a little function to concat two arrays and remove the duplicates:

let a = ['06.12.2021', '07.12.2021', '19.12.2021', '20.12.2021', '01.03.2022', '03.03.2022', '06.12.2021', '09.01.2022'];
let b = ['17.11.2021', '04.01.2022', '08.01.2022', '09.01.2022'];

let ab = [...new Set( a.concat( b ) )];

console.log(ab);

This works quite nice, but actually they are in a complete bad order. At least, they should start with the lowest date and ends with the highest. I've tried a sort function without any success:

let a = ['06.12.2021', '07.12.2021', '19.12.2021', '20.12.2021', '01.03.2022', '03.03.2022', '06.12.2021', '09.01.2022'];
let b = ['17.11.2021', '04.01.2022', '08.01.2022', '09.01.2022'];

let ab = [...new Set(a.concat(b))];

console.log(ab);

ab.sort((a, b) => Date.parse(b) - Date.parse(a));

console.log(ab);

Do you have any advice for me?

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

This is happening because some of the dates are not parseable in their current format and so can't be placed in the correct order. To test this yourself, try:

let a = ['06.12.2021', '07.12.2021', '19.12.2021', '20.12.2021', '01.03.2022', '03.03.2022', '06.12.2021', '09.01.2022'];
let b = ['17.11.2021', '04.01.2022', '08.01.2022', '09.01.2022'];

let ab = [...new Set(a.concat(b))];

let test = ab.map(m => new Date(m))

console.log(test);

This is because JavaScript dates are not equipped for handling DD.MM.YYYY formatted dates. If you don't mind using a library, something like DateJs may help

about 4 years ago · Santiago Gelvez Relatório

0

I added a custom parse function that parses dates explicitly formatted as DD.MM.YYYY into Date objects. This fixes the date parse issue. I also changed b - a to a - b which sorts in increasing order.

let a = ['06.12.2021', '07.12.2021', '19.12.2021', '20.12.2021', '01.03.2022', '03.03.2022', '06.12.2021', '09.01.2022'];
let b = ['17.11.2021', '04.01.2022', '08.01.2022', '09.01.2022'];

let ab = [...new Set(a.concat(b))];

console.log(ab);

function parse(date) {
    const [d, m, y] = date.split('.');
    return new Date(y, m-1, d);
}

ab.sort((a, b) => parse(a) - parse(b));

console.log(ab);

about 4 years ago · Santiago Gelvez Relatório

0

With split() and sort() you can archive this:

const a = ['06.12.2021', '07.12.2021', '19.12.2021', '20.12.2021', '01.03.2022', '03.03.2022', '06.12.2021', '09.01.2022'];
const b = ['17.11.2021', '04.01.2022', '08.01.2022', '09.01.2022'];

const ab = [...new Set( a.concat( b ) )];

const newDates = ab.sort(function(a,b){
  return new Date(b.date) - new Date(a.date);
}).map(e => {
  const a = e.split(".");
  return a[2] + "." + a[1] + "." + a[0]
}).sort().map(e => {
  const a = e.split(".");
  return a[2] + "." + a[1] + "." + a[0]
})

console.log('newDates',newDates)

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda