我想要下面的代码降序排序类型:

这是升序排序:

public static <T extends Vm> void sortByCpuUtilization(List<T> vmList) {
    Collections.sort(vmList, new Comparator<T>() {

        @Override
        public int compare(T a, T b) throws ClassCastException {
            Double aUtilization = a.getTotalUtilizationOfCpuMips(CloudSim.clock());
            Double bUtilization = b.getTotalUtilizationOfCpuMips(CloudSim.clock());
            return bUtilization.compareTo(aUtilization);
        }
    });
}

最佳答案

将a与b进行比较:

public static <T extends Vm> void sortByCpuUtilization(List<T> vmList) {
    Collections.sort(vmList, new Comparator<T>() {

        @Override
        public int compare(T a, T b) throws ClassCastException {
            Double aUtilization = a.getTotalUtilizationOfCpuMips(CloudSim.clock());
            Double bUtilization = b.getTotalUtilizationOfCpuMips(CloudSim.clock());
            return aUtilization.compareTo(bUtilization);
        }
    });
}

09-08 08:13