Sort List in java

Asked

Viewed 13,756 times

5

I have a List<Pessoa> where the attributes of the object are: Name, age, address, etc. I have a screen where I insert people in this list and I submit a report, I would like to display this object sorted by Name. How can I do that?

4 answers

8


Can implement the Comparable interface and make objects of the person class comparable to other objects of the same type:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Pessoa implements Comparable<Pessoa>  {
    private String nome;
    private int idade;
    private String endereco;

    //getters and setters

    @Override
    public int compareTo(Pessoa p) {
        return this.getNome().compareTo(p.getNome());
    }

    @Override
    public String toString() {
        return "Pessoa [nome=" + nome + ", idade=" + idade + ", endereco="
                + endereco + "]";
    }

    public static void main(String[] args) {
        Pessoa p1 = new Pessoa();
        p1.setNome("João");
        Pessoa p2 = new Pessoa();
        p2.setNome("Maria");
        Pessoa p3 = new Pessoa();
        p3.setNome("André");
        List<Pessoa> pessoas = new ArrayList();
        pessoas.add(p1);
        pessoas.add(p2);
        pessoas.add(p3);
        Collections.sort(pessoas);
        System.out.println(pessoas);        
    }
}

Making it possible to then call the method Collections.sort(pessoas);.

Upshot:

[Person [name=Andrew, age=0, address=null]
, Person [name=John, age=0, address=null]
, Person [name=Maria, age=0, address=null]
]

  • 2

    Thank you very much Math, it worked, I had been trying to use the Collections.sort(); but did not know that had to implement an interface

3

So I find it even easier: - Using java 8 Reference Method

Collections.sort(listaPessoas, Comparator.comparing(Pessoa::getNome));

Hugs!!

3

List<Pessoa>  pessoas = new ArrayList<Pessoa>();

// setando
Pessoa pessoa;
for(int i=0;i<100;i++)
{
  pessoa = new pessoa();
  pessoa.setNome(...);
  pessoas.add(pessoa);
}

//Ordenando
Collections.sort(pessoas, new Comparator<Pessoa>() {
        @Override
        public int compare(Pessoa  pessoa1, Pessoa  pessoa2)
        {

            return  pessoa1.Nome.compareTo(pessoa2.Nome);
        }
    });

2

I guess I’ve already set the attributes so I’ll just leave the method that does the ordering:

compare accepts a @Override where you can implement your sorting.

Your class needs to implement the interface Comparator

  private static void ordenaPorNome(List<Pessoa> listaPessoas) {  
        Collections.sort(listaPessoas, new Comparator<Pessoa>() {  
            @Override  
            public int compare(Pessoa p1, Pessoa p2) {  
                return p1.getNome().compareTo(p2.getNome());  
            }  

     });  
    }  
  • You said that it needs to implement the Comparable class but in your example you implement the Comparator class. The two work, each in its own way, but I believe that would make it clearer. Also, you could change the List<Lista> for List<Pessoa>.

  • I didn’t mind, I’ll get ready

Browser other questions tagged

You are not signed in. Login or sign up in order to post.