2

So I've got a list announceList which have a list of Announces, I'm trying to sort the list ascending for a percentage value, so if I got the list this way:

Announce1 - 20.10%
Announce2 - 20.54%
Announce3 - 27.50%
Announce4 - 76.36%

After the sorting I want to have the list like this:

Announce4 - 76.36%
Announce3 - 27.50%
Announce2 - 20.54%
Announce1 - 20.10%

The method I'm using is this:

Collections.sort(announceList, new Comparator<Announce>() {
   @Override
   public int compare(Announce o1, Announce o2) {
       int i1 = o1.getMatchPercentage().intValue();
       int i2 = o2.getMatchPercentage().intValue();
       return Integer.compare(i1, i2);
   }
});

adapter.setAnnounceList(announceList);
adapter.notifyDataSetChanged();

But I don't get the list how I wanted, please if you know what I'm I doing wrong tell me, any help will be apreciated, thank you.

6
  • 4
    Are you using intValue() to compare two float/double values? Commented May 24, 2018 at 17:56
  • Yes, is that a problem? I understand that doing it I'm losting the decimal numbers but the number sort still not working Commented May 24, 2018 at 17:57
  • 1
    From the data you show, you want to sort the data in descending order so try return Integer.compare(i2, i1); Commented May 24, 2018 at 17:58
  • @DavidZam the comparator you are using sorts from smallest to largest. If you negate the result of the Integer.compare call, then you will sort by largest to smallest. Commented May 24, 2018 at 17:58
  • Or simply, return o2.getMatchPercentage().compareTo(.getMatchPercentage()); Commented May 24, 2018 at 18:00

3 Answers 3

2

You need to compare the actual double/float values if you want the decimal places to affect your sorting i.e. avoid 74.5 > 74.9. Also since you want the sorting to be in descending order you can try using Double.compare():

Collections.sort(announceList, new Comparator<Announce>() {
   @Override
   public int compare(Announce o1, Announce o2) {
       return Double.compare(o2.getMatchPercentage(), o1.getMatchPercentage());
   }
});

If you are using Java 8 or higher you can also do:

announceList.sort(Comparator.comparingDouble(Announce::getMatchPercentage).reversed());

Here is an small example using plain doubles:

public static void main(String[] args) {
    List<Double> list = new ArrayList<Double>();
    list.add(20.10);
    list.add(20.54);
    list.add(27.50);
    list.add(76.36);

    System.out.println("Before sort:");
    for(Double d : list) {
        System.out.println(d);
    }
    System.out.println();

    Collections.sort(list, new Comparator<Double>() {
        @Override
        public int compare(Double o1, Double o2) {
            return Double.compare(o2, o1);
        }
    });

    System.out.println("After sort:");
    for(Double d : list) {
        System.out.println(d);
    }
}

Output:

Before sort:
20.1
20.54
27.5
76.36

After sort:
76.36
27.5
20.54
20.1
Sign up to request clarification or add additional context in comments.

6 Comments

Now it works but the double comparison is now working, I've got in the list 20.10% before 20.54%
@DavidZam can you post what Announce looks like? I'm curious regarding what getMatchPercentage() does
Announce class has a private Double matchPercentage; var and one getter and setter for this var: public Double getMatchPercentage() { return this.matchPercentage; } public void setMatchPercentage(Double matchPercentage) { this.matchPercentage = matchPercentage; }
@DavidZam ok thanks for sharing the implementation it makes sense. The sorting should work, I've added a simple example using just plain Doubles. Maybe there's something else in the Announce
It was another problem, I've solved it, thank you so much for your help @StaticBeagle
|
2

Since Java 8 there is a way more conveniant way to perform this sort:

announceList.sort(Comparator.comparingDouble(Announce::getMatchPercentage).reversed());

Omitt the reversed() to sort the list in ascending order instead.

This makes use of the comparingDouble() method which was new in Java 8.

Comments

0

From the Java documentation of Comparator::compare

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

So you in order to sort ascending you need to invert the order of your parameters for Integer.compare, like:

Integer.compare(i2, i1);

Or as suggested in the comment:

return o1.getMatchPercentage().intValue() -o2.getMatchPercentage().intValue();

However, for precision, you should use Double.compare

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.