How do I call Arrays.sort (array, new methodOrd ()) in Java?

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 to learn and be able to sort an array of objects by their name in my class that implements Comparator .

    
asked by anonymous 17.09.2014 / 16:26

1 answer

6

To order an array of objects in Java, we usually use ready-made methods of class java.util.Arrays .

Being T a certain type of object (class), the most common method contains the following signature:

Arrays.sort(T[], Comparator<? extends T>)

Primitive types and Strings do not need a Comparator , since there are already overloaded versions of sort for this.

But, for example, if you have a Cliente class, you should then pass an array of clients ( Cliente[] ) and an implementation of Comparator of type 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 .java file normally.

Example

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;
    }

}

Creating an array

From the above class, we can create any array:

Cliente[] clientes = {
        new Cliente("Bruno", 30),
        new Cliente("Maria", 28),
        new Cliente("Carlos", 40)
};

Sorting by nome

Now see 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 was done above is to simply implement the compare method of the Comparator interface and delegate execution to the compareTo method of the nome attribute, which is String .

Version with lambda expression (Java 8)

In Java 8, with lambdas , things get even easier:

Arrays.sort(clientes, (Cliente b1, Cliente b2) 
    -> b1.getNome().compareTo(b2.getNome()));

Sorting by idade

If we wanted, for example, to sort 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 compare method refers to an individual comparison between any two clients of the vector. This method is used internally by the sort algorithm.

Return 1 means that the first element is greater than the second. The -1 return is the opposite. And the% return of% means that they are the same for this comparison criterion.

Version with lambda expression (Java 8)

Let's also look at the second ordering example 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;
});

Considerations

Note that the array is modified by the 0 method, so if you want to keep the original array you need to make a copy. To make an array copy simply and efficiently, use the static method sort .

The algorithm used in the System.arraycopy() method is known as timsort , described this link .

The sample functional code is available in my GitHub .

    
17.09.2014 / 17:06