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);
Very good, helped me a lot thanks.
– pedrobornio
do not forget to accept the answer if it helped resolve ;)
– Ricardo Pontual