0

I have an ArrayList with 3 Objects in it. Each Object is a new book in my case. Each book has different values for the title, retail price, quantity in stock, etc.

How do I compare and sort the retail price for all of the objects. I have heard of

Collections.sort(books, new Comparator<BookInfo>(){
    @Override
    public int compare(BookInfo book1, BookInfo book2) {
        return book1.getTitle().compareTo(book2.getTitle());
    }
});

with a custom Comparator, but I cannot use the compareTo method for doubles/ints. How would I implements this, or change it so I can compare doubles/ints?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 1
    Just use `<`, `==`, and `>` for comparing primitive values. – rgettman Feb 19 '14 at 00:44
  • Yes this will work for only comparing two values, but what I need is to compare all the values and sort them. –  Feb 19 '14 at 00:46
  • @user3325783: to sort a list you just need a function that compares two values... maybe read some more about sort algorithms? – vanza Feb 19 '14 at 00:48

3 Answers3

2

You can do this always

Assumption : price value is Double

   Collections.sort(books, new Comparator<BookInfo>(){

    @Override
    public int compare(BookInfo book1, BookInfo book2) {
        return book1.getPrice().compareTo(book2.getPrice());
    }
});

Reference : How to sort an array of ints using a custom comparator?

Community
  • 1
  • 1
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
  • I don't think this will work because you can't derefence doubles. And the price is a double –  Feb 19 '14 at 01:16
1

try casting to class Double and then compare:

Collections.sort(books, new Comparator<BookInfo>(){

        @Override
        public int compare(BookInfo book1, BookInfo book2) {
            Double p1 = book1.getRetailPrice();
            Double p2 = book2.getRetailPrice();
            return p1.compareTo(p2);
        }
    });

If you want to sort first after Price and second after Name:

Collections.sort(books, new Comparator<BookInfo>(){

        @Override
        public int compare(BookInfo book1, BookInfo book2) {
            Double p1 = book1.getRetailPrice();
            Double p2 = book2.getRetailPrice();
            int res = p1.compareTo(p2);
            if(res == 0){
                return book1.getTitle().compareTo(book2.getTitle());
            }
            return res;
        }
    }); 
nidomiro
  • 820
  • 2
  • 10
  • 24
1

You can implement the Comparator however you want, you only need to return a correct value, depending on comparison of the two objects:

  • negative integer if book1 < book2
  • positive integer if book1 > book2
  • 0 if book1 == book2

    Collections.sort(books, new Comparator<BookInfo>() {
        @Override
    
        public int compare(BookInfo book1, BookInfo book2) {
            if (book1.getPrice() < book2.getPrice()) {
                return -1;
            } else if (book1.getPrice() > book2.getPrice()) {
                return 1;
            } else return 0;
        }
    });
    

This way you can customize the behaviour of Comparator anyway you like.

Warlord
  • 2,798
  • 16
  • 21