collections.sort排序使用

使用匿名函数的方式对result数据进行排序:

Collections.sort(result, new Comparator<WorkerVO>() {
                @Override
                public int compare(WorkerVO o1, WorkerVO o2) {
                    int serviceCount1 = o1.getServiceCount();
                    int serviceCount2 = o2.getServiceCount();
                    long restMinutes1 = o1.getRestMinutes();
                    long restMinutes2 = o2.getRestMinutes();

                    if (0 == rankTyepe) {//服务人数降序
                        int compare = Integer.compare(serviceCount2, serviceCount1); 
                        return compare==0?Long.compare(restMinutes1, restMinutes2):compare;
                    } else if (1 == rankTyepe) { //服务人数升序
                        int compare = Integer.compare(serviceCount1, serviceCount2);
                        return compare==0?Long.compare(restMinutes2, restMinutes1):compare; `//表示,如果服务人数相等就按空闲时间排序(多条件排序的应用和)`
                    }
                    return 0;
                }
            });

java8:对list排序

 list.sort((a, b) -> Integer.compare(a.age, b.getAge()));
 assignWorkerDOList.sort((a, b) -> Long.compare(a.getServiceStartDate().getTime(), b.getServiceStartDate().getTime()));

参考:https://blog.csdn.net/j277699931/article/details/49660607

发布了57 篇原创文章 · 获赞 0 · 访问量 4577

猜你喜欢

转载自blog.csdn.net/Arry_Coding/article/details/102852122