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

162
Visualizações
Sorting array doesn't work properly when some items have unknown values

Say I have array of objects

[
  {
      "name": "John", 
      "height": "172", 
      "weight": "87"
  }, 
  {
      "name": "Tom", 
      "height": "167", 
      "weight": "67"

  }, 
  {
      "name": "James", 
      "height": "156", 
      "weight": "uknown"
  }, 
  {
      "name": "Jeremy", 
      "height": "unknown", 
      "weight": "85"
  }, 
]

Now if I want to sort them by height I can do persons.sort((a, b) => b.height - a.height); likewise if I want to sort them by weight I can do the same but with weight persons.sort((a, b) => b.weight - a.weight);

So far this would work fine, however some of the values are unknown, and if I'm holding this array in some state and sort them by either the unknown values mess it up and it doesn't sort properly. In a case where there are alot more objects that 4, it sorts to some point but as soon as it hits an object with an unknown property it messes it up and the rest of the array is then not sorted properly. How can I overcome this? Perhaps doing something pushing the items with unknown value at the bottom?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

All you have to do is handle the specific cases when height/weight is unknown and return the appropriate value

For example:

const items = [
  {
      "name": "John", 
      "height": "172", 
      "weight": "87"
  }, 
  {
      "name": "Tom", 
      "height": "167", 
      "weight": "67"

  }, 
  {
      "name": "James", 
      "height": "156", 
      "weight": "uknown"
  }, 
  {
      "name": "Jeremy", 
      "height": "unknown", 
      "weight": "85"
  }, 
    {
      "name": "Jeremy2", 
      "height": "162", 
      "weight": "85"
  }, 
    {
      "name": "Jeremy3", 
      "height": "unknown", 
      "weight": "85"
  }, 
];

items.sort((a,b) => {
  if(b.height === 'unknown' && a.height ==='unknown') return 0;
  else if(b.height === 'unknown') return -1;
  else if(a.height === 'unknown') return 1;
  else return a.height - b.height;
});

console.log(items);

The comparator function compare(a, b) works like this:

if return value sort > 0, sort b before a.
else if return value sort < 0, sort a before b.
else if return value === 0  keep original order of a and b.

MDN Link

Edit:

If one is dealing with comma seperated strings, one convert to number first:

items.sort((a,b) => {
  if(b.height === 'unknown' && a.height ==='unknown') return 0;
  else if(b.height === 'unknown') return -1;
  else if(a.height === 'unknown') return 1;
  else return parseFloat(a.height.replace(/,/g, '')) -
parseFloat(b.height.replace(/,/g, ''))
});

Source

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