Ordenando List<>s facilmente
Autor: welton silva | Publicado: 2014-09-27T16:29:00-03:00

Saudações a todos.

Hoje vou demonstrar como ordenar um objeto List facilmente sem a necessidade de reinventar a roda.
Falo do Objeto Collections que contém o método sort e que vai facilitar sua vida.

Vamos ao exemplo:

public static void main(String[] args) 
{ 

&nbsp;&nbsp;&nbsp;&nbsp; //Preenchendo listas de palavras
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; List&lt;String&gt; listaPalavras = new ArrayList&lt;String&gt;();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listaPalavras.add("teste");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listaPalavras.add("veste");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listaPalavras.add("cego");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listaPalavras.add("pote");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listaPalavras.add("luzes");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listaPalavras.add("123");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listaPalavras.add("Nozes");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listaPalavras.add("luzes");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listaPalavras.add("fundos");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Antes como era sem ordenar
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(listaPalavras);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Ordenando com o método estático Collections ordenada sem case sensitive
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Collections.sort(listaPalavras, String.CASE_INSENSITIVE_ORDER);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Depois de ordenar
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(listaPalavras);
}




Muito simples. nesse primeiro exemplo usamos uma lista de String e foi muito fácil.


Agora vamos ver o caso de uma Lista de clientes simples.




&nbsp; public static void main(String[] args)
&nbsp;{

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;List&lt;Cliente&gt; lista1 = new ArrayList&lt;Cliente&gt;();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //instânciando e adicionando
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cliente cliente1 = new Cliente();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente1.setCodigo(1);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente1.setNome("A");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente1.setSobrenome("Silva");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;lista1.add(cliente1);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Cliente cliente2 = new Cliente();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente2.setCodigo(2);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente2.setNome("d");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente2.setSobrenome("Silva");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;lista1.add(cliente2);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Cliente cliente3 = new Cliente();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente3.setCodigo(3);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente3.setNome("M");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente3.setSobrenome("Silva");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;lista1.add(cliente3);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Cliente cliente4 = new Cliente();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente4.setCodigo(4);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente4.setNome("F");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente4.setSobrenome("Silva");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;lista1.add(cliente4);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Cliente cliente5 = new Cliente();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente5.setCodigo(5);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente5.setNome("M");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cliente5.setSobrenome("Silva");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;lista1.add(cliente5);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //ordenando
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Collections.sort(lista1);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //imprimindo
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(Cliente c :lista1)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println(c.getNome());
&nbsp;&nbsp;&nbsp; 
&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp; 
Ops. ocorre erro ao implementar o método sort da classe Collections ok?
Faltou a implementação na Classe pojo Clientes

package model;

public class Cliente implements Comparable&lt;Cliente&gt; {

&nbsp;&nbsp;&nbsp; private int codigo;
&nbsp;&nbsp;&nbsp; private String nome;
&nbsp;&nbsp;&nbsp; private String sobrenome;
&nbsp;&nbsp;&nbsp; public int getCodigo() {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return codigo;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public void setCodigo(int codigo) {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.codigo = codigo;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public String getNome() {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return nome;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public void setNome(String nome) {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.nome = nome;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public String getSobrenome() {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return sobrenome;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public void setSobrenome(String sobrenome) {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.sobrenome = sobrenome;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; @Override
&nbsp;&nbsp;&nbsp; public int compareTo(Cliente o) {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return this.getNome().toLowerCase().compareTo(o.getNome().toLowerCase());
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;implementando a classe Comparable e sobrescrevendo o método compareTo com o método irá funcionar.
Você pode colocar o LowerCase() também caso queira que não seja casesensitive.
Por enquanto é só.
Abraços;