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

170
Visualizações
Sorting "day" "week" "month" and "year"

I have an array of objects from an API

[
{path: 'image', location: 'LOCATION_1', time: 'day'}
1: {path: 'image', location: 'LOCATION_2', time: 'week'}
2: {path: 'image', location: 'LOCATION_1', time: 'year'}
3: {path: 'image', location: 'LOCATION_1', time: 'week'}
4: {path: 'image', location: 'LOCATION_1', time: 'month'}
5: {path: 'image', location: 'LOCATION_2', time: 'day'}
6: {path: 'image', location: 'LOCATION_2', time: 'month'}
7: {path: 'image', location: 'LOCATION_2', time: 'year'}
]

I want to sort them based on time but when I do, it goes like

day
month
week
year

Is there any way to make it like:

day
week
month
year

I am using functional component in javascript.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In the sorting function, you are comparing with the string that's why you are getting a sorted list with alphabetic order. If you want a custom order you can create a map to follow the order.

const data = [
  {path: 'image', location: 'LOCATION_1', time: 'day'},
  {path: 'image', location: 'LOCATION_2', time: 'week'},
  {path: 'image', location: 'LOCATION_1', time: 'year'},
  {path: 'image', location: 'LOCATION_1', time: 'week'},
  {path: 'image', location: 'LOCATION_1', time: 'month'},
  {path: 'image', location: 'LOCATION_2', time: 'day'},
  {path: 'image', location: 'LOCATION_2', time: 'month'},
  {path: 'image', location: 'LOCATION_2', time: 'year'},
];

const timeMap = {
  day: 0,
  week: 1,
  month: 2,
  year: 3,
}

data.sort((a, b) => {
  return timeMap[a.time] - timeMap[b.time];
});

console.log(data);

about 4 years ago · Juan Pablo Isaza Relatório

0

use the sort method like this
mdn : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

let arr = 
[
{path: 'image', location: 'LOCATION_1', time: 'day'},
{path: 'image', location: 'LOCATION_2', time: 'week'},
{path: 'image', location: 'LOCATION_1', time: 'year'},
{path: 'image', location: 'LOCATION_1', time: 'week'},
{path: 'image', location: 'LOCATION_1', time: 'month'},
{path: 'image', location: 'LOCATION_2', time: 'day'},
{path: 'image', location: 'LOCATION_2', time: 'month'},
{path: 'image', location: 'LOCATION_2', time: 'year'}
];
let sortBy = ["day", "week", "month", "year"]
console.log(arr.sort((a,b) => sortBy.indexOf(a.time) - sortBy.indexOf(b.time)))

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