-2

I need to sort a java list containing objects of type Hotel

List<Hotel> hotelList = new ArrayList<>();

Inside the class I do have the method

    @Override
public List<Room> getAvailableRooms() {

    return this.rooms;
}

I need to sort my hotelList by the price attribute found in Room class. Any suggestions?

1 Answers1

0

You should either use a Comparator or implement the Comparable interface

public class Foo implements Comparable<ToSort> {

private int val;

public Foo(int val){
    this.val = val;
}

@Override
public int compareTo(ToSort f) {

    if (val > f.val) {
        return 1;
    }
    else if (val <  f.val) {
        return -1;
    }
    else {
        return 0;
    }

}

Read more here https://dzone.com/articles/sorting-java-arraylist

Ryan Sangha
  • 442
  • 6
  • 17