java中根据集合中的汉字进行排序(汉字数字)
需要对list集合中的description字段进行排序,description是汉字且包含汉字数字,如下:
(图片来源网络,侵删)
new LegLists("来源-西宁", "第三百一十四赛段"), new LegLists("平安-来源", "第一千三百一十四赛段"), new LegLists("平安-西宁", "第六十三赛段")
根据第二个字段,第几赛段进行排序。
整体代码:
public static void main(String[] args) { List legLists = Arrays.asList( new LegLists("来源-西宁", "第三百一十四赛段"), new LegLists("平安-来源", "第一千三百一十四赛段"), new LegLists("平安-西宁", "第六十三赛段") ); Collections.sort(legLists, new Comparator() { @Override public int compare(LegLists o1, LegLists o2) { int num1 = extractNumber(o1.getDescription()); int num2 = extractNumber(o2.getDescription()); return Integer.compare(num1, num2); } private int extractNumber(String s) { Matcher matcher = Pattern.compile("[零一二三四五六七八九十百千万]+").matcher(s); if (matcher.find()) { return chineseNumberToInt(matcher.group()); } return 0; } private int chineseNumberToInt(String chineseNumber) { Map chineseToArabic = new HashMap(); chineseToArabic.put('零', 0); chineseToArabic.put('一', 1); chineseToArabic.put('二', 2); chineseToArabic.put('三', 3); chineseToArabic.put('四', 4); chineseToArabic.put('五', 5); chineseToArabic.put('六', 6); chineseToArabic.put('七', 7); chineseToArabic.put('八', 8); chineseToArabic.put('九', 9); chineseToArabic.put('十', 10); chineseToArabic.put('百', 100); chineseToArabic.put('千', 1000); chineseToArabic.put('万', 10000); int result = 0; int temp = 0; int lastUnit = 1; for (char ch : chineseNumber.toCharArray()) { int num = chineseToArabic.getOrDefault(ch, -1); if (num != -1) { if (num >= 10) { // it's a unit like 十,百,千,万 if (temp == 0) { temp = 1; } result += temp * num; temp = 0; lastUnit = num; } else { temp = temp * 10 + num; } } } result += temp * lastUnit; return result; } }); for (LegLists lists : legLists) { System.out.println(lists); } } static class LegLists { /** * 赛段名称 */ private String legName; public LegLists(String legName, String description) { this.legName = legName; this.description = description; } /** * 对应的是第几赛段(截图文字描述用) */ private String description; public String getLegName() { return legName; } public void setLegName(String legName) { this.legName = legName; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "LegLists{" + "legName='" + legName + '\'' + ", description='" + description + '\'' + '}'; } }
下面是我抽出来的工具类,用于提取汉字中的中文数字,并转成数字:
public class ChineseNumber { public static int extractNumber(String s) { Matcher matcher = Pattern.compile("[零一二三四五六七八九十百千万]+").matcher(s); if (matcher.find()) { return chineseNumberToInt(matcher.group()); } return 0; } public static int chineseNumberToInt(String chineseNumber) { Map chineseToArabic = new HashMap(); chineseToArabic.put('零', 0); chineseToArabic.put('一', 1); chineseToArabic.put('二', 2); chineseToArabic.put('三', 3); chineseToArabic.put('四', 4); chineseToArabic.put('五', 5); chineseToArabic.put('六', 6); chineseToArabic.put('七', 7); chineseToArabic.put('八', 8); chineseToArabic.put('九', 9); chineseToArabic.put('十', 10); chineseToArabic.put('百', 100); chineseToArabic.put('千', 1000); chineseToArabic.put('万', 10000); int result = 0; int temp = 0; int lastUnit = 1; for (char ch : chineseNumber.toCharArray()) { int num = chineseToArabic.getOrDefault(ch, -1); if (num != -1) { if (num >= 10) { // it's a unit like 十,百,千,万 if (temp == 0) { temp = 1; } result += temp * num; temp = 0; lastUnit = num; } else { temp = temp * 10 + num; } } } result += temp * lastUnit; return result; } }
剩下的排序就简单了
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。