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

62
Vistas
Javascript - Sort object by multiple values

var data = {
  "input": [{
      "countA": 1,
      "countB": 10
    },
    {
      "countA": 15,
      "countB": 13
    },
    {
      "countA": 26,
      "countB": 24
    },
    {
      "countA": 6,
      "countB": 25
    },
    {
      "countA": 15,
      "countB": 20
    }
  ]
};

var sorted = data.input.sort(function(a, b) {
  return a['countB'] < b['countB'] ? 1 : -1;
});

console.log(sorted);

The outcome after the first sorting should be after another sorting:

[
    {
        "countA": 6,
        "countB": 25
    },
    {
        "countA": 15,
        "countB": 20
    },
    {
        "countA": 1,
        "countB": 10
    }
    {
        "countA": 26,
        "countB": 24
    },
    {
        "countA": 15,
        "countB": 13
    }
]

So, it should be the highest of "countB" and then descending as long as "countB" is higher than "countA". So far I tried multiple ways, but there's no outcome so far.

Thanks for any help!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could sort by the result of the comparison of countB > countA and then by the value of countB.

const
    data = [{ countA: 1, countB: 10 }, { countA: 15, countB: 13 }, { countA: 26, countB: 24 }, { countA: 6, countB: 25 }, { countA: 15, countB: 20 }];

data.sort((a, b) =>
    (b.countB > b.countA) - (a.countB > a.countA) ||
    b.countB - a.countB
);

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can first extract the values of the array when "countb" >= "countA", sort that array then add the remaining values at the end (note that for order is kept for when "countb" < "countA") :

var data = {
  "input": [{
      "countA": 1,
      "countB": 10
    },
    {
      "countA": 15,
      "countB": 13
    },
    {
      "countA": 26,
      "countB": 24
    },
    {
      "countA": 6,
      "countB": 25
    },
    {
      "countA": 15,
      "countB": 20
    }
  ]
};

const ElementsToSort = data.input.filter(elem => elem.countA <= elem.countB);
const RemainingElements = data.input.filter(elem => ElementsToSort.indexOf(elem) < 0);

// sort as you did
const PartiallySorted = ElementsToSort.sort(function(a, b) {
  return a['countB'] < b['countB']
          ? 1
          : a['countB'] > b['countB']
            ? -1
            : 0;
});


//add the remaining values
const sorted = PartiallySorted.concat(RemainingElements);
console.log(sorted);

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