Arraylist Comparator JSP Object

Asked

Viewed 173 times

2

I’m having trouble implementing the Comparator method, to sort the Object Arraylist.

I have an Enterprise class. Where I then create an Arraylist, which is filled from a content manager system.

I added the method compareTo, and it sorts by the Area attribute. But now I also need you to sort by number of rooms, and bathrooms.

How could I do that ?

Here is the current cider:

    <%! 
public class Empreendimento implements Comparable<Empreendimento>{

     //possui atributos, gets, sets e construtor

        public int compareTo(Empreendimento Emp) {
            if (this.menorArea < Emp.getMenorArea()) {
                return -1;
            }
            if (this.menorArea > Emp.getMenorArea()) {
                return 1;
            }
            return 0;
        }
%>

After the content manager, create the Arraylist with these obejtos. I make a filter, and then I order. Then I use this:

Collections.sort(ArrayResultadoBusca);
  • would like to add an Addendum on Arraylist: here. I believe I can help you understand your question.

1 answer

3

With Java 8, there is a simple and clean solution to handle multiple comparisons.

Lambda:

Comparator<Empreendimento> comparator = Comparator
        .comparing(e -> e.getMenorArea)
        .thenComparingInt(e -> e.getQuantidadeQuarto)
        .thenComparingInt(e -> e.getQuantidadeBanheiro);

Method Reference:

Comparator<Empreendimento> comparator = Comparator
        .comparing(Empreendimento::getMenorArea)
        .thenComparingInt(Empreendimento::getQuantidadeQuarto)
        .thenComparingInt(Empreendimento::getQuantidadeBanheiro);

It is not recommended to mix code html of view coded Java, it is interesting to make the comparison in a class and then pass the result by a Expression Language (EL).

An alternative is to implement a Comparator that instead of passing the attributes by parameter, passes two classes of the same type and compares their attributes.

Ex:

Collections.sort(lista, new Comparator<Empreendimento>(){
    public int compare(Empreendimento e1, Empreendimento e2) {
        int comparacao = e1.getMenorArea().compareTo(e2.getMenorArea());
        if(comparacao != 0) {
           return comparacao;
        }

        comparacao = e1.getQuantidadeQuarto().compareTo(e2.getQuantidadeQuarto());
        if(comparacao != 0) {
           return comparacao;
        }

        return e1.getQuantidadeBanheiro().compareTo(e2.getQuantidadeBanheiro());     
    }
});

It is important to implement the equals and hashCode in the model class, in that case Entrepreneurship.

  • This last example I didn’t understand. I can create the compare inside the class ? Can you have several compare ? Ex.: compareQuarto(), compareArea()... And then make the ordering call? What would the ordering call look like? And in the Enterprise class, I need to use the " Comparator Implements " ?

  • I edited the post with the changes. You can implement if you want, the anonymous class was just for example.

Browser other questions tagged

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