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

181
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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