Multiple sorts with Arraylist

Asked

Viewed 428 times

2

I have a question. I want to order the Arraylist person in different ways.

<%@ page import= "java.util.*"%>

<%! 
public class Pessoa implements Comparable<Pessoa> {  
    String Nome;  
    Integer numero; 

    public  Lista(String n,int m){  
        this.Nome = n;  
        this.numero = m;  
    }  

    public String getNome(){
        return Nome;
    }  
    public void SetNome(String Nome){
        this.Nome = Nome;
    }   
    public Integer getArea(){
        return numero;
    }     
    public void SetArea(int numero){
        this.numero = numero;
    }  
    public String toString(){  
        return this.Nome+" "+this.numero;
    }

    // Para ordenar por nome 
    private static void ordenaPorNome(List<Pessoa> lista) {
    Collections.sort(lista, new Comparator<Pessoa>() {
        @Override
        public int compare(Lista o1, Lista o2) {
            return o1.getNome().compareTo(o2.getNome());
        }
    });
    }
    // Para ordenar por numeros
    private static void ordenaPorNumero(List<Pessoa> lista) {
    Collections.sort(lista, new Comparator<Pessoa>() {
        @Override
        public int compare(Lista o1, Lista o2) {
            return o1.getArea().compareTo(o2.getArea());
        }
    });
    }       

}

%>


<%

    List<Pessoa> lista = new ArrayList<Pessoa>();  
    Lista t1,t2,t3; 
    t1 = new Lista("a",3);  
    t2 = new Lista("b",2);  
    t3 = new Lista("c",1); 

    lista.add(t1);  
    lista.add(t2);  
    lista.add(t3);

    //como chamar o metodo de ordenação aqui ?
    ordenaPorNome(lista);
    System.out.println(lista);

    //como chamar o metodo de ordenação aqui ?
    ordenaPorNumero(lista);
    System.out.println(lista);

%>

I’m using JSP, and it’s all in the same file.

Ai will later have an option for the user to choose how to sort, and also add more attributes in the person class to be sorted.

2 answers

2

You must use the interface Comparator. And in turn invoke the method Collection.Sort(List, Comparator);

For example

Collection.sort(list, new Comparator<Pessoa>() {
    public int compare(Pessoa p1, Pessoa p2) {
        return  p1.numero.compareTo(p2.numero);
    }
});

For each attribute you must create the Comparator. In Java 8 you can use Lambda expression. If you need examples let us know.

  • I could not put this example of yours inside a method, and then call the method outside the class, to sort according to the parameters I receive ?

  • 1

    Yes, you can implement it that way. I would do something like this: 1. Create all the likely Comparators. 2. Create a Map, with all of them. 3. With the key, I would search for the Commander on the map. Any speech there. Looking at your code, it would be nice to take from within jsp pages.

1

Suggestion: Change the sorting methods to public instead of private, so you can use anywhere else.

To make the call, the methods are static and are inside the right Person?

Then it would look like this:

    Pessoa.ordenaPorNome(lista);
    System.out.println(lista);


   Pessoa.ordenaPorNumero(lista);
    System.out.println(lista);

Browser other questions tagged

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