Mounting ranking with sorting criteria with array

Asked

Viewed 112 times

2

I need to sort this Array initially by points and if there is a tie, those indexes of the array that tied would be unpacked in the games, if it remained tied would jump to tiebreak and if it continued would go to age.

What I have achieved so far is to sort by the points and identify the indexes with the points tied.

If anyone knows an easier way to develop this type of ranking, I am very grateful.

    const obj = [
    {nome: "Elinete",   apelido: "elinete",     pontos: "99",   games:"200",    tiebreak:"4",   idade:"30"},
    {nome: "Luna",      apelido: "lunajessica", pontos: "82",   games:"180",    tiebreak:"9",   idade:"35"},
    {nome: "Thamires",  apelido: "tata",        pontos: "50",   games:"130",    tiebreak:"3",   idade:"40"},
    {nome: "Alex",      apelido: "alex",        pontos: "50",   games:"150",    tiebreak:"-1",  idade:"45"},
    {nome: "Hugo",      apelido: "hugobornio",  pontos: "50",   games:"181",    tiebreak:"10",  idade:"19"},
    {nome: "Aparecido", apelido: "aparecido",   pontos: "30",   games:"120",    tiebreak:"3",   idade:"18"},
    {nome: "Jaime",     apelido: "jaime",       pontos: "30",   games:"120",    tiebreak:"3",   idade:"18"},
    {nome: "André",     apelido: "andré",       pontos: "30",   games:"120",    tiebreak:"3",   idade:"18"},
    {nome: "João",      apelido: "joão",        pontos: "30",   games:"120",    tiebreak:"3",   idade:"18"},
    {nome: "moacir",    apelido: "moacir",      pontos: "10",   games:"100",    tiebreak:"2",   idade:"21"}

    ]

    obj.sort(function (a, b) {
    return b.pontos.localeCompare(a.pontos);
    });

    const indexPontosIguais = []

    function logArrayElements(element, index, array) {


    if(index > 0){

        const pontosAnterior = obj[index-1].pontos
        const pontosAtual = element.pontos

            if(pontosAtual == pontosAnterior){

                const indexAtual = index
                const indexAnterior = index-1

                indexPontosIguais.push(indexAnterior, indexAtual)

            }
         }
     }

     obj.forEach(logArrayElements);

1 answer

2


I think the function sort is more practical for this.
Basically she does foreach in all elements, passing two by two to compare, where we should:

  • return -1 if the first is smaller
  • return 1 if the second is smaller
  • returns 0 if both are equal

Dai is possible to make a Function to validate points and games:

const obj = [
    {nome: "Elinete",   apelido: "elinete",     pontos: "99",   games:"200",    tiebreak:"4",   idade:"30"},
    {nome: "Luna",      apelido: "lunajessica", pontos: "82",   games:"180",    tiebreak:"9",   idade:"35"},
    {nome: "Thamires",  apelido: "tata",        pontos: "50",   games:"130",    tiebreak:"3",   idade:"40"},
    {nome: "Alex",      apelido: "alex",        pontos: "50",   games:"150",    tiebreak:"-1",  idade:"45"},
    {nome: "Hugo",      apelido: "hugobornio",  pontos: "50",   games:"181",    tiebreak:"10",  idade:"19"},
    {nome: "Aparecido", apelido: "aparecido",   pontos: "30",   games:"120",    tiebreak:"3",   idade:"18"},
    {nome: "Jaime",     apelido: "jaime",       pontos: "30",   games:"120",    tiebreak:"3",   idade:"18"},
    {nome: "André",     apelido: "andré",       pontos: "30",   games:"120",    tiebreak:"3",   idade:"18"},
    {nome: "João",      apelido: "joão",        pontos: "30",   games:"120",    tiebreak:"3",   idade:"18"},
    {nome: "moacir",    apelido: "moacir",      pontos: "10",   games:"100",    tiebreak:"2",   idade:"21"}

    ]

const sortFunc = (a, b) => {
  // verica se pontos são iguais, para desempatar por games
  if (a.pontos == b.pontos) {
  	// retorna:
  	// -1 se a < b, 1 se a > b, 0 se a = b
     return a.games < b.games
       ? -1
       : a.games > b.games
         ? 1
         : 0;
  }
  
  // mesma lógica do anterior
   return a.pontos < b.pontos
       ? -1
       : a.pontos > b.pontos
         ? 1
         : 0;
  
}

var sorted = obj.sort(sortFunc);

console.log(sorted);

  • Very good, helped me a lot thanks.

  • do not forget to accept the answer if it helped resolve ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.