You are looking for the interface Comparator
. This interface gives you the ability to sort a collection in countless different ways. Its advantage over the Comparable
is that you do not need to modify the class whose objects you want to sort, so you can even use it in classes that are not modifiable.
Suppose the class Pessoa
:
class Pessoa {
private String nome;
private int idade;
private float peso;
public Pessoa(String nome, int idade, float peso) {
super();
this.nome = nome;
this.idade = idade;
this.peso = peso;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
public float getPeso() {
return peso;
}
public void setPeso(float peso) {
this.peso = peso;
}
@Override
public String toString() {
return "Pessoa [nome=" + nome + ", idade=" + idade + ", peso=" + peso + "]";
}
}
It has three different attributes and you want to sort a list of objects of type Pessoa
each hour using one of the attributes. You will need to create a class for each attribute you want to use based on the comparison, so:
class OrdenaPorNome implements Comparator<Pessoa> {
@Override
public int compare(Pessoa um, Pessoa dois) {
return um.getNome().compareTo(dois.getNome());
}
}
class OrdenaPorIdade implements Comparator<Pessoa> {
@Override
public int compare(Pessoa um, Pessoa dois) {
return um.getIdade() - dois.getIdade();
}
}
class OrdenaPorPeso implements Comparator<Pessoa> {
@Override
public int compare(Pessoa um, Pessoa dois) {
if(um.getPeso() > dois.getPeso()) return 1;
if(um.getPeso() < dois.getPeso()) return -1;
return 0;
}
}
When creating a class that implements Comparator
you are required to implement the method compare()
the expected arguments of which are of the generic type informed among the <>
. Notice that the implementation I made above involves three different types: one String
, one int
and a float
.
To the String
I used the method itself compareTo()
the class implements. For the int
I subtracted one by the other. For the float
I made the necessary comparisons to return a coherent value, because it is not possible to subtract because the method waits as a return int
and we would have problems with rounding.
Now it’s just to get it all going:
public class Comparando {
public static void main(String[] args) {
List<Pessoa> pessoas = new ArrayList<Pessoa>();
pessoas.add(new Pessoa("Pedro", 25, 71));
pessoas.add(new Pessoa("Maria", 27, 60));
pessoas.add(new Pessoa("João", 30, 75.5f));
pessoas.add(new Pessoa("Fernanda", 40, 55));
System.out.println("Ordenando por nome:");
Collections.sort(pessoas, new OrdenaPorNome());
System.out.println(pessoas);
System.out.println("Ordenando por idade:");
Collections.sort(pessoas, new OrdenaPorIdade());
System.out.println(pessoas);
System.out.println("Ordenando por peso:");
Collections.sort(pessoas, new OrdenaPorPeso());
System.out.println(pessoas);
}
}
Exit:
Ordering by name:
[Person [name=Fernanda, age=40, weight=55.0], Person [name=João, age=30, weight=75.5], Person [name=Maria, age=27, weight=60.0], Person [name=Pedro, age=25, weight=71.0]]
Ordering by age:
[Person [name=Peter, age=25, weight=71.0], Person [name=Mary, age=27, weight=60.0], Person [name=John, age=30, weight=75.5], Person [name=Fernanda, age=40, weight=55.0]]
Ordering by weight:
[Person [name=Fernanda, age=40, weight=55.0], Person [name=Maria, age=27, weight=60.0], Person [name=Peter, age=25, weight=71.0], Person [name=John, age=30, weight=75.5]]
Welcome to Stackoverflow in English. Show us the code you have done so far, without it it is difficult to analyze the problem to know if you are going the right way or not. Here on the site we prefer objective questions, so try to shape your problem very well, otherwise the question can be suspended for being too wide.
– Rafael Almeida