1
How to sort objects through the name attribute? I am implementing the Comparator interface.
I made a small example.
Let’s go to the codes:
file: Person.java
import java.util.ArrayList;  
import java.util.List;  
public abstract class Pessoa {  
    protected String nome;  
    protected int telefone;  
    protected int matricula;  
    private static int contadorMatricula;  
    private static int atribuirMatricula() {  
        Pessoa.contadorMatricula++;  
        return Pessoa.contadorMatricula;  
    }  
}  
file: Personal Physics.java
public class PessoaFisica extends Pessoa {  
    protected int cpf;  
    public PessoaFisica(String nome, int telefone, int cpf) {  
        this.nome=nome;  
        this.telefone=telefone;  
        this.cpf=cpf;  
    }  
}  
main file: Main.java
import java.util.ArrayList;  
import java.util.Collections;  
import java.util.List;  
import java.util.Comparator;  
public class Main implements Comparator<PessoaFisica> {  
    @Override  
    public int compare(PessoaFisica pessoa1, PessoaFisica pessoa2) {  
        return pessoa1.nome.compareTo(pessoa2.nome);  
        }  
    public static void main(String args[]) {  
        List pessoasFisicas = new ArrayList<>();  
        PessoaFisica pessoa1=new PessoaFisica("André Nascimento", 321, 654);  
        pessoasFisicas.add(pessoa1);  
        PessoaFisica pessoa2=new PessoaFisica("Tiago Santos", 123, 456);  
        pessoasFisicas.add(pessoa2);      
    }      
}  
As I show to the Collections.sort which is to order by name?
Note: I know you do not need to implement the Comparator interface, because the correct is to instantiate and already implement at runtime, example: new Comparator<PessoaFisica>(); 
but I’m doing it the least ideal way to facilitate my understanding.
Format that code there young.
– user28595
That one answer should help.
– josivan