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

87
Visualizações
I want to sort the string values of Object's Key in a given order. What would be the most optimized way?

I want to sort "ONAIR", "READY", "CLOSING", "CLOSED" order in JavaScript. Is it possible string value of Object's key in map or sort of JavaScript?

as is :

const list = [
{status: 'READY'},
{status: 'CLOSED'},
{status: 'ONAIR'},
{status: 'CLOSING'},
];

to be:

const list = [
{status: 'ONAIR'},
{status: 'READY'},
{status: 'CLOSING'},
{status: 'CLOSED'},
];
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If I understand what you mean correctly. Try this:

const sortSequencely = ["ONAIR", "READY", "CLOSING", "CLOSED"];
const list = [
    {status: 'READY'},
    {status: 'CLOSED'},
    {status: 'ONAIR'},
    {status: 'CLOSING'},
];
console.log('Original list', list);
const listSorted = list.sort((a,b) => {
    const indexA = sortSequencely.findIndex(x=>x === a.status);
    const indexB = sortSequencely.findIndex(x=>x === b.status);
    return indexA - indexB;
})
console.log('Sorted list', listSorted)

about 4 years ago · Juan Pablo Isaza Relatório

0

One way to achieve this is with a dictionary which specifies the order you desire; then you can sort based on looking up the status values in that dictionary:

const list = [
  {status: 'READY'},
  {status: 'CLOSED'},
  {status: 'ONAIR'},
  {status: 'CLOSING'},
];

const order = { 'ONAIR' : 0, 'READY' : 1, 'CLOSING' : 2, 'CLOSED' : 3 }

list.sort((a, b) => order[a.status] - order[b.status]);

console.log(list)

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