java list 根据vo里面的字段排序

 // 排序方法
    public static void sortList(List<VO> fadfList, String sortBy, String sortOrder) {
        Comparator<VO> comparator;
        switch (sortBy) {
            case "a":
                comparator = Comparator.comparing(FADF::getA);
                break;
            case "b":
                comparator = Comparator.comparing(FADF::getB);
                break;
            case "c":
                comparator = Comparator.comparingDouble(fadf -> Double.parseDouble(fadf.getC()));
                break;
            default:
                throw new IllegalArgumentException("Invalid sortBy parameter");
        }

        // 根据sortOrder选择升序或者降序
        if ("desc".equalsIgnoreCase(sortOrder)) {
            comparator = comparator.reversed();
        }

        // 使用Comparator对List进行排序
        Collections.sort(fadfList, comparator);
    }

你可能感兴趣的:(java,list,windows)