Java Comparator字符排序(数字、字母、中文混合排序)

Java.lang.Character类 复习一下

这是修正前的排序效果:

 

这是修正后的排序效果:

 

 完整示例:

以下是排序的部份代码(非全部代码:拼音首字母算法不在其中)

 
  
  1. import java.util.Arrays;  
  2. import java.util.Comparator;  
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. public class Demo {  
  7.   
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.         String fileNames[] = { "fss01", "fss2", "fss01_22", "fss3", "fss1", "fss10", "fss20", "fss4", "fss30", "fss21", "fss12","fss01_3" };  
  11.         char chFileNames[][] = new char[fileNames.length][];  
  12.         String[] oldSortedNames = new String[fileNames.length];  
  13.         for (int i = 0; i < fileNames.length; i++) {  
  14.             chFileNames[i] = fileNames[i].toCharArray();  
  15.             oldSortedNames[i] = fileNames[i];  
  16.         }  
  17.   
  18.         // Arrays.sort(fileNames, StrLogicCmp);  
  19.         Arrays.sort(chFileNames, ChsLogicCmp);  
  20.         System.out.println("_Random_" + "\t" + "_Tradion_" + "\t" + "_Target_");  
  21.         String line;  
  22.         for (int i = 0; i < fileNames.length; i++) {  
  23.             line = fileNames[i] + (fileNames[i].length() >= 8 ? "\t" : "\t\t");  
  24.             line += oldSortedNames[i] + (oldSortedNames[i].length() >= 8 ? "\t" : "\t\t");  
  25.             line += new String(chFileNames[i]);  
  26.             System.out.println(line);  
  27.               
  28.         }  
  29.           
  30.           
  31.     }  
  32.       
  33.     static Comparator<String> StrLogicCmp = new Comparator<String>() {  
  34.   
  35.         @Override  
  36.         public int compare(String o1, String o2) {  
  37.             // TODO Auto-generated method stub  
  38.             return 0;  
  39.         }  
  40.           
  41.     };  
  42.       
  43.     // "f01s2s22", "f1s02s2"  
  44.     static Comparator<char[]> ChsLogicCmp = new Comparator<char[]>() {  
  45.         class Int{  
  46.             public int i;  
  47.         }  
  48.         public int findDigitEnd(char[] arrChar, Int at) {  
  49.             int k = at.i;  
  50.             char c = arrChar[k];  
  51.             boolean bFirstZero = (c == '0');  
  52.             while (k < arrChar.length) {  
  53.                 c = arrChar[k];  
  54.                 //first non-digit which is a high chance.  
  55.                 if (c > '9' || c < '0') {  
  56.                     break;  
  57.                 }  
  58.                 else if (bFirstZero && c == '0') {  
  59.                     at.i++;   
  60.                 }  
  61.                 k++;  
  62.             }  
  63.             return k;  
  64.         }  
  65.   
  66.         @Override  
  67.         public int compare(char[] a, char[] b) {  
  68.             if(a != null || b != null){  
  69.                 Int aNonzeroIndex = new Int();  
  70.                 Int bNonzeroIndex = new Int();  
  71.                 int aIndex = 0, bIndex = 0,   
  72.                 aComparedUnitTailIndex, bComparedUnitTailIndex;  
  73.       
  74. //              Pattern pattern = Pattern.compile("D*(d+)D*");  
  75. //              Matcher matcher1 = pattern.matcher(a);  
  76. //              Matcher matcher2 = pattern.matcher(b);  
  77. //              if(matcher1.find() && matcher2.find()) {  
  78. //                  String s1 = matcher1.group(1);  
  79. //                  String s2 = matcher2.group(1);  
  80. //              }  
  81.                       
  82.                 while(aIndex < a.length && bIndex < b.length){  
  83.                     //aIndex <   
  84.                     aNonzeroIndex.i = aIndex;  
  85.                     bNonzeroIndex.i = bIndex;  
  86.                     aComparedUnitTailIndex = findDigitEnd(a, aNonzeroIndex);  
  87.                     bComparedUnitTailIndex = findDigitEnd(b, bNonzeroIndex);  
  88.                     //compare by number   
  89.                     if (aComparedUnitTailIndex > aIndex && bComparedUnitTailIndex > bIndex)  
  90.                     {  
  91.                         int aDigitIndex = aNonzeroIndex.i;  
  92.                         int bDigitIndex = bNonzeroIndex.i;  
  93.                         int aDigit = aComparedUnitTailIndex - aDigitIndex;  
  94.                         int bDigit = bComparedUnitTailIndex - bDigitIndex;  
  95.                         //compare by digit   
  96.                         if(aDigit != bDigit)  
  97.                             return aDigit - bDigit;  
  98.                         //the number of their digit is same.  
  99.                         while (aDigitIndex < aComparedUnitTailIndex){  
  100.                             if (a[aDigitIndex] != b[bDigitIndex])  
  101.                                 return a[aDigitIndex] - b[bDigitIndex];  
  102.                             aDigitIndex++;  
  103.                             bDigitIndex++;  
  104.                         }  
  105.                         //if they are equal compared by number, compare the number of '0' when start with "0"   
  106.                         //ps note: paNonZero and pbNonZero can be added the above loop "while", but it is changed meanwhile.  
  107.                         //so, the following comparsion is ok.  
  108.                         aDigit = aNonzeroIndex.i - aIndex;  
  109.                         bDigit = bNonzeroIndex.i - bIndex;  
  110.                         if (aDigit != bDigit)  
  111.                             return aDigit - bDigit;  
  112.                         aIndex = aComparedUnitTailIndex;  
  113.                         bIndex = bComparedUnitTailIndex;  
  114.                     }else{  
  115.                         if (a[aIndex] != b[bIndex])  
  116.                             return a[aIndex] - b[bIndex];  
  117.                         aIndex++;  
  118.                         bIndex++;  
  119.                     }  
  120.                       
  121.                 }  
  122.                   
  123.             }  
  124.             return a.length - b.length;  
  125.         }  
  126.   
  127.     };  
  128. }

 

转载于:https://www.cnblogs.com/interdrp/p/8970593.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于 Java字母排序的问题。我可以为您提供一些相关的代码和思路。 在 Java 中,可以使用 Comparator 接口来实现对字符串的排序Comparator 接口有一个 compare() 方法,可以按照指定的规则比较两个对象的大小并返回一个整数值。我们可以自定义一个 Comparator 对象来实现字符串的排序。 在对字符串进行排序时,一般是按照字典序进行排序,即先按照第一个字符的 ASCII 码值进行比较,如果相同则继续比较第二个字符,依次类推,直到比较出大小为止。但是,如果字符串中既有字母又有数字中文字符,就需要按照一定的规则进行排序,这时就需要自定义一个 Comparator 对象。 下面是一个示例代码,可以实现数字字母中文混合排序: ```java import java.text.Collator; import java.util.Comparator; import java.util.Locale; public class MyComparator implements Comparator<String> { private Collator collator = Collator.getInstance(Locale.CHINA); @Override public int compare(String o1, String o2) { // 如果两个字符串相等,则返回0 if (o1.equals(o2)) { return 0; } // 如果两个字符串均为数字,则按照数字大小排序 if (o1.matches("\\d+") && o2.matches("\\d+")) { return Integer.compare(Integer.parseInt(o1), Integer.parseInt(o2)); } // 如果两个字符串均不为数字,则按照字典序排序 if (!o1.matches("\\d+") && !o2.matches("\\d+")) { return collator.compare(o1, o2); } // 如果一个字符串为数字,一个字符串为非数字,则返回非数字字符串大 if (o1.matches("\\d+")) { return 1; } else { return -1; } } } ``` 在这个示例代码中,我们首先使用 Collator 类来比较中文字符的大小,然后根据字符串的类型进行排序。如果两个字符串均为数字,则按照数字大小进行排序;如果两个字符串均不为数字,则按照字典序进行排序;如果一个字符串为数字,一个字符串为非数字,则返回非数字字符串大。 使用这个自定义的 Comparator 对象进行排序时,只需要调用 Collections.sort() 方法并传入该对象即可: ```java List<String> list = new ArrayList<>(); list.add("3"); list.add("a"); list.add("1"); list.add("中文"); Collections.sort(list, new MyComparator()); System.out.println(list); ``` 输出结果为:[1, 3, a, 中文] 希望这个示例代码能够帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值