Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda