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

229
Visualizações
Sort an array by a list of it's fields

Let's say I have an array of objects:

Objects = [
{ "id": 1, "name": Joseph, function: "preacher"},
{ "id": 2, "name": Ann, function: "singer"},
{ "id": 3, "name": Miles, function: "preacher"},
{ "id": 4, "name": Jack, function: "singer"},
{ "id": 5, "name": Igor, function: "secretary"}
];

And also an array of properties:

sort = ['function', 'name'];

I have to sort the Objects array, using a combination of properties(sort array). So I did it like this:

const intlCollator = new Intl.Collator('pt-BR', { usage: 'sort' });

  Objects.sort(
    (x, y) =>
      (intlCollator.compare(x[sort[0]], y[sort[0]])) ||
      (intlCollator.compare(x[sort[1]], y[sort[1]])) ||
      (intlCollator.compare(x[sort[2]], y[sort[2]]))
    );

How would I make the sorting dynamic? I mean, iterate using variable sort combinations. For example:

sort = ['function', 'name'];

Or:

sort = ['name'];
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could iterate the keys until a comparing returns a not falsy value.

const
    objects = [{ id: 1, name: "Joseph", function: "preacher" }, { id: 2, name: "Ann", function: "singer" }, { id: 3, name: "Miles", function: "preacher" }, { id: 4, name: "Jack", function: "singer" }, { id: 5, name: "Igor", function: "secretary" }],
    intlCollator = new Intl.Collator('pt-BR', { usage: 'sort' }),
    sort = ['function', 'name'];


objects.sort((a, b) => {
    let r;
    sort.some(k => r = intlCollator.compare(a[k], b[k]));
    return r;
});
    
console.log(objects);

about 4 years ago · Juan Pablo Isaza Relatório

0

Since ES10 sort is stable. That means you can first sort using the first key, then sort the second and so on.

const Objects = [
{ "id": 1, "name": "Joseph", function: "preacher"},
{ "id": 2, "name": "Ann", function: "singer"},
{ "id": 3, "name": "Miles", function: "preacher"},
{ "id": 4, "name": "Jack", function: "singer"},
{ "id": 5, "name": "Igor", function: "secretary"}
];

const sort = ['name', 'function'];

const intlCollator = new Intl.Collator('pt-BR', { usage: 'sort' });
sort.forEach(s => {
  Objects.sort((l, r) => intlCollator.compare(l[s], r[s]));
});

console.log(Objects);

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