1
Setting:
I am developing a small application as a task of my course. As the intention of the work is precisely to show what we learned during the classes, I can not escape much of what I already have, better to say, I can not leave for "very advanced things". Although I know it’s naturally complicated to do what I want, I want tips in "simple" ways to do it.
At a certain point I need to sort a collection of users by their wins (from the largest pro minor), using as tie-breaker its defeats (the less defeats, the higher). The class Usuario
has both attributes (vitorias
and derrotas
).
What I want to do is also the ordination of array for amount of defeats user, however, without having to rewrite this entire method.
My current sorting method is as follows (the sorting algorithm used is the Bubble Sort)
private void ordenar(Usuario[] usuarios){
boolean troca = true;
while(troca){
troca = false;
for(int i = 0; i < usuarios.length - 1; i++) {
if(usuarios[i].getVitorias() < usuarios[i + 1].getVitorias() ||
(usuarios[i].getVitorias() == usuarios[i + 1].getVitorias()
&& usuarios[i].getDerrotas() > usuarios[i + 1].getDerrotas()))
{
Usuario u = usuarios[i + 1];
usuarios[i + 1] = usuarios[i];
usuarios[i] = u;
troca = true;
}
}
}
}
Good! I liked the solution.
– Jéf Bueno