Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对Java对象列表进行排序_Java_List_Sorting_Object - Fatal编程技术网

对Java对象列表进行排序

对Java对象列表进行排序,java,list,sorting,object,Java,List,Sorting,Object,我需要对包含类型为Hotel List<Hotel> hotelList = new ArrayList<>(); List hotelList=new ArrayList(); 在课堂上,我确实有这个方法 @Override public List<Room> getAvailableRooms() { return this.rooms; } @覆盖 公共列表getAvailableRooms(){ 把这个房间还给我; } 我需要

我需要对包含类型为Hotel

List<Hotel> hotelList = new ArrayList<>();
List hotelList=new ArrayList();
在课堂上,我确实有这个方法

    @Override
public List<Room> getAvailableRooms() {

    return this.rooms;
}
@覆盖
公共列表getAvailableRooms(){
把这个房间还给我;
}
我需要根据房间类中的
价格
属性对酒店列表进行排序。
有什么建议吗?

您应该使用比较器或实现可比较的接口

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;
    }

}
public类Foo实现了可比较的{
私人国际旅行社;
公共食品(国际){
this.val=val;
}
@凌驾
公共int比较(ToSort f){
如果(val>f.val){
返回1;
}
否则如果(val
在这里阅读更多

哪种价格?…价格的可能重复是Room class的一个属性