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

177
Vistas
How to sort by a specific value with localeCompare?

I want to sort my current array by having live at the front, instead of sorting it alphabetically so that the items with live will be at the front, followed by schedule and lastly end, but the way I'm doing it now with localeCompare, it will only sort alphabetically.

So how can I do so to sort the array in a specific way that I want?

Array Before Sorting Example

const [bulletins] = useState([
     {
       id: 1,
       liveStatus: 'live',
     },
     {
       id: 2,
       liveStatus: 'live',
     },
     {
       id: 3,
       liveStatus: 'end',
     },
     {
       id: 4,
       liveStatus: 'schedule',
     },
     {
       id: 5,
       liveStatus: 'end',
     }
]);

Sorted Array Example

const [bulletins] = useState([
     {
       id: 3,
       liveStatus: 'end',
     },
     {
       id: 5,
       liveStatus: 'end',
     },
     {
       id: 2,
       liveStatus: 'live',
     },
     {
       id: 1,
       liveStatus: 'live',
     },
     {
       id: 4,
       liveStatus: 'schedule',
     }
]);
const displayBulletins = bulletins
    .sort((a,b) => {
        return a.liveStatus.localeCompare(b.liveStatus)
    })
    .map((bulletin) => {
        return (
            <Fragment key={bulletin.id}>
                <BulletinList
                    bulletin={bulletin}
                />
            </Fragment>
        );
    })
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use indexOf to check the first letter in a list of sorted letters ('lse' in this case):

const bulletins = [{id: 1,liveStatus: 'live',},{id: 2,liveStatus: 'live',},{id: 3,liveStatus: 'end',},{id: 4,liveStatus: 'schedule',},{id: 5,liveStatus: 'end',}];

bulletins.sort((a, b) => 'lse'.indexOf(a.liveStatus[0]) - 'lse'.indexOf(b.liveStatus[0]));

console.log(bulletins);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Then no need to sort the elements, all you can use is reduce and arrange in the way you like

const arr = [
  {
    id: 1,
    liveStatus: "live",
  },
  {
    id: 2,
    liveStatus: "live",
  },
  {
    id: 3,
    liveStatus: "end",
  },
  {
    id: 4,
    liveStatus: "schedule",
  },
  {
    id: 5,
    liveStatus: "end",
  },
];

const dict = { live: 0, schedule: 1, end: 2 };
const resultObj = Array.from({ length: Object.keys(dict).length + 1 }, () => []);


const result = arr
  .reduce((acc, curr) => {
    acc[dict[curr.liveStatus] ?? resultObj.length - 1].push(curr);
    return acc;
  }, resultObj)
  .flat();

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */

.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

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