JAVA list 根据对象的某个属性排序

//需要排序的list
List<T> list= new ArrayList<>();

//开始排序
Collections.sort(list, new Comparator<T>(){
            public int compare(T o1, T o2) {
                //排序属性
                if(o1.getNum() < o2.getNum()){
                    return 1;
                }
                if(o1.getNum() == o2.getNum()){
                    return 0;
                }
                return -1;
            }
        });   

//排序完成  输出
System.out.println(new Gson().toJson(list))

猜你喜欢

转载自www.cnblogs.com/codigup/p/10172171.html