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

114
Vistas
Sorting a array based on a specific value

Hi I'm working on a game application. The schema of the game database looks like this

[
  {
    UserName,
    UserImage,
    UserScore
  }
]

Here is an example snippet. I would like to sort the json array based on the score and extract the top 10(if any).

[{
    name: "user1",
    image: "image",
    score: 10
  },
  {
    name: "user2",
    image: "image",
    score: 167
  },
  {
    name: "user3",
    image: "image",
    score: 1
  },
  {
    name: "user4",
    image: "image",
    score: 102
  },
  {
    name: "user5",
    image: "image",
    score: 12
  }
]

I'm having trouble sorting this array based on the userScore so that I could display the top 10 on the leaderboards. Any help would be greatly appreciated.

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

0

try it for sort by number:

 const sortNum = (arr) => {
    arr.sort(function (a, b) {
      return a - b;
    });
    return arr;
  };

in your case:

  const sortNum = (arr) => {
        arr.sort(function (a, b) {
          return a.score - b.score;
        });
        return arr;
      };
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