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. }

 

2021-11-23更新算法--排序时不区分字母大小写

这是之前的算法:

更新后的效果:

 

posted @ 2018-04-29 10:50  锐洋智能  阅读(37176)  评论(3编辑  收藏  举报