6
Knowing that I have a class with an array of objects and another class that implements the interface Comparator
.
I would like any example just so I can learn and sort an array of objects by their name in my implementing class Comparator
.
6
Knowing that I have a class with an array of objects and another class that implements the interface Comparator
.
I would like any example just so I can learn and sort an array of objects by their name in my implementing class Comparator
.
6
To order a array of objects in Java, ready class methods are generally used java.util.Arrays
.
Being T
a certain object type (class), the most common method contains the following signature:
Arrays.sort(T[], Comparator<? extends T>)
Primitive Types and Strings Don’t Need a Comparator
, there are already overloaded versions of sort
for that reason.
But, for example, if you have a class Cliente
, you should then pass an array of clients (Cliente[]
) and an implementation of Comparator
of the kind T
or some subclass of T
, which is the meaning of ? extends T
.
The implementation of Comparator
can be done as an anonymous class or in a file .java
normally.
Cliente.java
public class Cliente {
private String nome;
private int idade;
public Cliente(String nome, int idade) {
this.nome = nome;
this.idade = idade;
}
public String getNome() {
return nome;
}
public int getIdade() {
return idade;
}
}
From the above class, we can create any array:
Cliente[] clientes = {
new Cliente("Bruno", 30),
new Cliente("Maria", 28),
new Cliente("Carlos", 40)
};
nome
See now an example of sorting by nome
:
Arrays.sort(clientes, new Comparator<Cliente>() {
@Override
public int compare(Cliente b1, Cliente b2) {
//TODO testar nulos
return b1.getNome().compareTo(b2.getNome()); //delegar para comparador da String
}
});
What has been done above is simply to implement the method compare
of the interface Comparator
and delegate the execution to the method compareTo
of the attribute nome
, that is String
.
In Java 8, using Amble, it just gets easier:
Arrays.sort(clientes, (Cliente b1, Cliente b2)
-> b1.getNome().compareTo(b2.getNome()));
idade
If we wanted, for example, to order by idade
, we could change the implementation as follows:
Arrays.sort(clientes, new Comparator<Cliente>() {
@Override
public int compare(Cliente b1, Cliente b2) {
//TODO testar nulos
if (b1.getIdade() > b1.getIdade()) return 1;
if (b1.getIdade() < b2.getIdade()) return -1;
return 0;
}
});
Note that the method compare
refers to an individual comparison between any two clients of the vector. This method is used internally by the algorithm of the sort
.
The return 1
means that the first element is greater than the second. The return -1
is the opposite. And the return 0
means that they are equal for this comparison criterion.
Let’s also see the second example of sorting also in Java 8:
Arrays.sort(clientes, (Cliente b1, Cliente b2) -> {
if (b1.getIdade() > b1.getIdade()) return 1;
if (b1.getIdade() < b2.getIdade()) return -1;
return 0;
});
Note that the array is modified by the method sort
, so if you want to keep the original array you need to make a copy. To make an array copy easily and efficiently, use the static method System.arraycopy()
.
The algorithm used in the method sort
is known as timsort, described at this link.
The functional code of the example is available in my Github.
Browser other questions tagged java array
You are not signed in. Login or sign up in order to post.
No words to thank for the help.
– Pedro Carvalho