Use Double in compareTo method

4

I have a question.

I have several methods of comparison, however, one of the attributes used to make the comparison is double.

There is an error:

  

Can not invoke compareTo (double) on the primitive type double

I tried converting the double to String . But he commands wrong.

It sorts by "alphabetical" order.

Example, sort the values like this:

10, 1000, 1002, 20, 30 , 300, 40.

When it was time to sort this out:

10, 20 ,30 ,40 , 300, 1000 e 1002.

Here is my code:

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

// ignorar os construtores, métodos e atributos

// Para ordenar por Menor Preco
    public static void ordenaPorMenorPreco(List<Empreendimento> ArrayResultadoBusca) {
        Collections.sort(ArrayResultadoBusca, new Comparator<Empreendimento>() {
            //@Override
            public int compare(Empreendimento emp1, Empreendimento emp2) {
                return emp1.getPrecoMenor().compareTo(emp2.getPrecoMenor());
            }
        });
    }

%>
    
asked by anonymous 26.02.2016 / 18:19

3 answers

4

Autoboxing is not helping.

Try to do this:

return Double.valueOf(emp1.getPrecoMenor()).compareTo(Double.valueOf(emp2.getPrecoMenor()));

I hope it helps.

    
26.02.2016 / 18:22
4

As you can see in the error message:

  

Can not invoke compareTo (double) on the primitive type double

You are trying to access a method in a primitive type, but primitive types have no methods, only the objects. You are working as if you were using the wrapper class object Double instead of the primitive type double .

You have three ways to solve this:

  • Change both the return of the getPrecoMenor method and the variable it returns; or
  • Make the conversion at the time you compare, as suggested in the other answer; or even
  • Instead of using the compareTo() method, you could do this:

    return emp1.getD() > emp2.getD() ? 1 : emp1.getD() < emp2.getD() ? -1 : 0 ;
    

Which is basically the same solution as @Thiago, but using two ternary conditional operators. It's less legible but it gets more compact, leaving it to the developer's discretion to best suit you.

The third option is therefore the compareTo() method, according to documentation , returns:

  

the value 0 if anotherDouble is numerically equal to this Double; a value less than 0 if this Double is numerically less than anotherDouble; and a value greater than 0 if this Double is numerically greater than anotherDouble.

In free translation, the method returns:

  

value 0 if the other double is numerically equal to that of the object that invoked the method; a value less than 0 if the value is less than the other double; and a value greater than 0 if it is larger than the other.

    
26.02.2016 / 18:23
3

Try this:

     Collections.sort(lst, new Comparator<Empreendimento>() {
        @Override
        public int compare(Empreendimento o1, Empreendimento o2) {
            if(o1.getPrecoMenor() > o2.getPrecoMenor()){
                return 1;
            }else if(o1.getPrecoMenor() < o2.getPrecoMenor()){
                return -1;
            }else 
                return 0;
        }
    });
    
26.02.2016 / 18:36