Scenery:
I'm developing a small application as a task for my course. As the intention of the work is precisely to show what we learned during the lessons, I can not escape much of what I already have, in other words, I can not go to "very advanced things". Although I know that it is of course complicated to do what I want, I want tips on "simple" ways of doing it.
At some point I need to sort a collection of users by their victories (from the highest pro minor), using as their tiebreak their defeats (the less defeats, higher). The Usuario
class has both attributes ( vitorias
and derrotas
).
What I want is to also sort the array by number of defeats from the user, however, without having to rewrite this entire method.
My current sort method is as follows (the sort algorithm used is 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;
}
}
}
}