java8 stream sorted 自定义排序

1.工具类,判断是否都是字符串中是否全是都是数字,判断字符串中是否含有数组

public class StringUtils {

/**

* 判断字符串是否全是数字

    * @param str

    * @return

    */

    public static boolean isInteger(String str) {

if(str ==null){

return false;

        }

Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");

        return pattern.matcher(str).matches();

    }

//截取数字

    public static StringgetNumbers(String content) {

Pattern pattern = Pattern.compile("\\d+");

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {

return matcher.group(0);

        }

return null;

    }

}

//先按gradeCode 升序 然后 按classCode 中的str 部分升序 最后按 classCode 中的num部分升序

数据源

public class ClassUtils {

public  static ListgetClassList() {

List classEntityList =new ArrayList<>();

        classEntityList.add(new ClassEntity("一年级A1班","A1","1","一年级"));

        classEntityList.add(new ClassEntity("一年级A2班","A2","1","一年级"));

        classEntityList.add(new ClassEntity("一年级A3班","A3","1","一年级"));

        classEntityList.add(new ClassEntity("一年级A4班","A4","1","一年级"));

        classEntityList.add(new ClassEntity("一年级1班","1","1","一年级"));

        classEntityList.add(new ClassEntity("一年级22班","22","1","一年级"));

        classEntityList.add(new ClassEntity("一年级2班","2","1","一年级"));

        classEntityList.add(new ClassEntity("一年级11班","11","1","一年级"));

        classEntityList.add(new ClassEntity("一年级A11班","A11","1","一年级"));

        classEntityList.add(new ClassEntity("一年级A22班","A22","1","一年级"));

        classEntityList.add(new ClassEntity("一年级B1班","B1","1","一年级"));

        classEntityList.add(new ClassEntity("一年级B22班","B22","1","一年级"));

        classEntityList.add(new ClassEntity("一年级B12班","B12","1","一年级"));

        classEntityList.add(new ClassEntity("二年级A1班","A1","2","二年级"));

        classEntityList.add(new ClassEntity("二年级A1班","A2","2","二年级"));

        classEntityList.add(new ClassEntity("二年级A1班","A11","2","二年级"));

        classEntityList.add(new ClassEntity("二年级A1班","B1","2","二年级"));

        classEntityList.add(new ClassEntity("二年级A1班","B21","2","二年级"));

        classEntityList.add(new ClassEntity("二年级A1班","B12","2","二年级"));

        return classEntityList;

    }

}

//排序

List classEntityList = ClassUtils.getClassList();

    Map> gradeList = classEntityList.stream().sorted(Comparator.comparing(ClassEntity::getGradeCode, (x, y) -> {

return x.compareTo(y);

    }).thenComparing(Comparator.comparing(ClassEntity::getClassCode, (codeX, codeY) -> {

if (StringUtils.isInteger(codeX) && StringUtils.isInteger(codeY)) {

return (Integer.valueOf(codeX) - Integer.valueOf(codeY)) >0 ?1 : (Integer.valueOf(codeX) - Integer.valueOf(codeY)) ==0 ?0 : -1;

        }else if (StringUtils.isInteger(codeX) && !StringUtils.isInteger(codeY)) {

return -1;

        }else if (!StringUtils.isInteger(codeX) && StringUtils.isInteger(codeY)) {

                  // 返回 1 codeX 和codeY位置对调

                  return 1;

        }else {

// 一年级 A11班 二年级 A22 班

            if (StringUtils.getNumbers(codeX) !=null && StringUtils.getNumbers(codeY) !=null) {

String numX = StringUtils.getNumbers(codeX);

                String numY = StringUtils.getNumbers(codeY);

                String strX = codeX.substring(0, codeX.indexOf(numX));

                String strY = codeY.substring(0, codeY.indexOf(numY));

                if (strX.compareTo(strY) !=0) {

return strX.compareTo(strY);

                }else {

return (Integer.valueOf(numX) - Integer.valueOf(numY)) >0 ?1 : (Integer.valueOf(numX) - Integer.valueOf(numY)) ==0 ?0 : -1;

                }

}

}

return -1;

    }))).collect(Collectors.groupingBy(ClassEntity::getGradeCode));

    for(Map.Entry> entry : gradeList.entrySet()) {

System.out.println(entry.getKey());

        entry.getValue().stream().forEach(item -> System.out.println(item));

    }

}

你可能感兴趣的:(java8 stream sorted 自定义排序)