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

144
Visualizações
Javascript sort array by date and alphabetical order

I want it to be sorted by date and alphabet in one sort how can i do that ? I think alphabetical order works good but date not works properly. Thanks for answers.

Data structure :

[{
    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",
  }],

Here my work :

    jsfiddle.net/pazyqb01/

And tried different solutions for sort date somehow i couldn't make it work.

Sorted Array shoul be like above :

 {
    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 Respostas
Responde à pergunta

0

this way:

simply follow the list of your sorting criteria

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 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