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

183
Visualizações
Sort list of items by specific status

I'd like to sort a list of task by status, for example, tasks with status "active" should be grouped follow by tasks with completed, and then tasks with other status should be displayed in the order they were added:

it should display something like this:

 const taskToShows = [
    {task: "task7", status: "active"},
    {task: "task6", status: "active"},
    {task: "task5", status: "active"},
    {task: "task8", status: "completed"},
    {task: "task4", status: "completed"},
    {task: "task1", status: "pending"},
    {task: "task2", status: "other status"},
    {task: "task3", status: "progress"},
   ]

My Approach was using sort() I can ordered at first but then it ordered all the tasks, and just need the actives and the completed ones at the top and All other tasks in the order added by the user

if(this.status === 'active' || this.status === 'completed'){  
      this.tasksToShow = this.tasksList.sort((a, b) => (a.status.toLowerCase() > 
      b.status.toLowerCase()) ? 1 : (a.status.toLowerCase()  === b.status.toLowerCase() ) 
      ? ((a.status > b.status) ? 0 : -1) : -1 )
     } 
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Of couse you сan use sort():

const taskToShows = [{task: "task7", status: "completed"},{task: "task6", status: "active"},{task: "task5", status: "active"},{task: "task8", status: "completed"},{task: "task4", status: "completed"},{task: "task1", status: "pending"},{task: "task2", status: "other status"},{task: "task3", status: "progress"},{task: "task33", status: "active"},{task: "task77", status: "completed"}];

const statusOrder = {active: 0, completed: 1};

const result = taskToShows.sort(
  ({status: s1}, {status: s2}) => (statusOrder[s1] ?? Infinity) - (statusOrder[s2] ?? Infinity)
);

console.log(taskToShows);
.as-console-wrapper{min-height: 100%!important; top: 0}

about 4 years ago · Santiago Trujillo Relatório

0

Try this-

 const taskToShows = [
    {task: "task7", status: "active"},
    {task: "task6", status: "active"},
    {task: "task5", status: "active"},
    {task: "task8", status: "completed"},
    {task: "task4", status: "completed"},
    {task: "task1", status: "pending"},
    {task: "task2", status: "other status"},
    {task: "task3", status: "progress"},
];



taskToShows.sort((a, b) => {
  const arr = ['active', 'completed'];
  return arr.includes(a.status) || arr.includes(b.status) ? 1 : -1;
});

console.log(taskToShows);
.as-console-wrapper{min-height: 100%!important; top: 0}

about 4 years ago · Santiago Trujillo Relatório

0

  • Using Array#reduce, iterate over the array while updating a Map whose initial values are three empty lists: active, completed, and other. In each iteration, get the corresponding list from the current status, other as the fallback, and push the current element to it.
  • Using Map#values, get the three lists
  • Using Array#flat, return a unified list where active shows first, then completed, then, other.

const taskToShows = [ {task: "task2", status: "other status"}, {task: "task7", status: "active"}, {task: "task5", status: "active"}, {task: "task8", status: "completed"}, {task: "task1", status: "pending"}, {task: "task3", status: "progress"}, {task: "task4", status: "completed"}, {task: "task6", status: "active"} ];

const res = [...
  taskToShows.reduce((lists, e) => {
    const list = lists.get(e.status) ?? lists.get('other');
    list.push(e);
    return lists;
  }, new Map([ ['active', []], ['completed', []], ['other', []] ]))
  .values()
].flat();

console.log(res);

about 4 years ago · Santiago Trujillo 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