Java字符串(包含字母和数字)通用排序

说明:本文章是之前查到的一篇安卓版的,具体原文路径忘记了。稍微改了一点,挺符合业务使用的!

一、看代码

/**
     * 包含数字的字符串进行比较(按照从小到大排序)
     */
    private static Integer compareString(String string1, String string2) {
        //拆分两个字符串
        List<String> list1 = splitString(string1);
        List<String> list2 = splitString(string2);
        //依次对比拆分出的每个值
        int index = 0;
        while (true) {
            //相等表示两个字符串完全相等
            if (index >= Math.max(list1.size(), list2.size())) {
                return 0;
            }
            String str1 = null;
            if (index < list1.size()){
                str1 = list1.get(index);
            }else{
                str1 =  "";
            }
            String str2 = null;
            if (index < list2.size()){
                str2 = list2.get(index);
            }else{
                str2 =  "";
            }
            //字符串相等则继续判断下一组数据
            if (str1.equals(str2)) {
                index++;
                continue;
            }
            //是纯数字,比较数字大小
            if (isNum(str1) && isNum(str2)) {
                if(Integer.parseInt(str1) < Integer.parseInt(str2)){
                    return -1;
                }else{
                    return 1;
                }
            }
            // 字符串比较大小
            if(str1.compareTo(str2)>0){
                return -1;
            }else{
                return 1;
            }
        }
    }
/**
     * 是否是纯数字
     */
    private static Boolean isNum(String str){
        return Pattern.compile("\\d+").matcher(str).matches();
    }
/**
     * 拆分字符串
     * 输入:第5章第100节课
     * 返回:[第,5,章第,100,节课]
     */
    private static List<String> splitString(String str){
        Matcher matcher = Pattern.compile("([^0-9]+)|(\\d+)").matcher(str);
        List<String> list = new ArrayList<>();
        while (matcher.find()) {
            list.add(matcher.group());
        }
        return list;
    }

二、看测试

List<String> strList0 = Arrays.asList(new String[]{
	       "4","2","5","1"
	});
	Collections.sort(strList0,(o1, o2) -> compareString(o1, o2));
	System.out.println(Arrays.asList(strList0));
	// 输出:[[1, 2, 4, 5]]
	
	List<String> strList1 = Arrays.asList(new String[]{
	       "GSM 1900","GSM 1800","GSM 850","GSM 900"
	});
	Collections.sort(strList1,(o1, o2) -> compareString(o1, o2));
	System.out.println(Arrays.asList(strList1));
	// 输出:[[GSM 850, GSM 900, GSM 1800, GSM 1900]]
	
	List<String> strList2 = Arrays.asList(new String[]{
	       "6G","2.4G","5G"
	});
	Collections.sort(strList2,(o1, o2) -> compareString(o1, o2));
	System.out.println(Arrays.asList(strList2));
	// 输出:[[2.4G, 5G, 6G]]
	
	List<String> strList3 = Arrays.asList(new String[]{
	       "n78","DC_12A_n2A","n8","DC_66A_n2A","DC_1A_n77A","DC_2A_n41A"
	});
	Collections.sort(strList3,(o1, o2) -> compareString(o1, o2));
	System.out.println(Arrays.asList(strList3));
	// 输出:[n8, n78, DC_1A_n77A, DC_2A_n41A, DC_12A_n2A, DC_66A_n2A]]

三、对象测试

@Data
public class SarKeyValVO {

    private Integer key;

    private String band;
}
List<SarKeyValVO> sarKeyValVOS = new ArrayList<>();
  sarKeyValVOS.add(new SarKeyValVO(0,"GSM 1900"));
  sarKeyValVOS.add(new SarKeyValVO(1,"GSM 1800"));
  sarKeyValVOS.add(new SarKeyValVO(2,"GSM 850"));
  sarKeyValVOS.add(new SarKeyValVO(3,"GSM 900"));
  
  List<SarKeyValVO> collect = sarKeyValVOS
  .stream()
  .sorted((o1, o2) -> compareString(o1.getBand(), o2.getBand()))
  .collect(Collectors.toList());
  
  System.out.println(Arrays.asList(collect));
  // {...GSM 850}, {...GSM 900}, {...GSM 1800}, {...GSM 1900}

你可能感兴趣的:(java,windows,开发语言)