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

 

Java Comparator字符排序(数字、字母、中文混合排序)的更多相关文章

  1. java只允许输入数字字母下划线中文

    public static void main(String[] args) { Pattern pattern = Pattern.compile("[_0-9a-z]+"); ...

  2. MySQL、Oracle、DB2等数据库常规排序、自定义排序和按中文拼音字母排序

    MySQL常规排序.自定义排序和按中文拼音字母排序,在实际的SQL编写时,我们有时候需要对条件集合进行排序. 下面给出3中比较常用的排序方式,mark一下 1.常规排序ASC DESC ASC 正序 ...

  3. JavaScript非数字(中文)排序

    直接上代码: var arr=[ {name:"张散步",age:"23",sports:"篮球",number:"231123& ...

  4. Jtable 表格按多列排序(支持中文汉字排序)

    这两天公司让做一个Jtable表格的排序,首先按A列排序,在A列相等时按B列排序,B列相等时按C列排序,ABC三列可以任意指定,最多分三列,这样的一个需求.由于我是大神,所以必须做了出来.ok,不自恋 ...

  5. Sql Server之ORDER BY不规则排序.如:中文月份排序

    ORDER BY CASE Month WHEN '一月' THEN 1 WHEN '二月' THEN 2 WHEN '三月' THEN 3 WHEN '四月' THEN 4 WHEN '五月' TH ...

  6. js 混合排序(类似中文手机操作系统中的通讯录排序)

    在阳光明媚最适合打盹的下午, 特意静音的手机竟然动起来了, 你没看错, 它震动了.... 上帝(顾客)来电, "报表查询系统左侧树状菜单中设备的中文名称不能排序", 要增加排序功能 ...

  7. mysql实现首字母从A-Z排序

    1.常规排序ASC DESC ASC 正序 DESC倒叙 -- 此处不用多讲 2.自定义排序 自定义排序是根据自己想要的特定字符串(数字)顺序进行排序.主要是使用函数 FIELD(str,str1,s ...

  8. JS排序:localeCompare() 方法实现中文排序、sort方法实现数字英文混合排序

    定义:用本地特定的顺序来比较两个字符串. 语法:stringObject.localeCompare(target) 参数:target——要以本地特定的顺序与 stringObject 进行比较的字 ...

  9. java 获取中文字符的首字母

    原理: GB2312编码中的中文是按照拼音排序的 注意: 一些生僻的字无法获得正确的首字母,原因是这些字都是后加入的. import java.io.UnsupportedEncodingExcept ...

随机推荐

  1. Android动画分类

    动画分类 View动画(补间动画).帧动画.属性动画 View动画(补间动画)包括:平移.旋转.缩放.透明度,View动画是一种渐近式动画 帧动画:图片切换动画 属性动画:通过动态改变对象的属性达到动 ...

  2. 从LeNet-5到DenseNet

    一篇不错的总结:https://zhuanlan.zhihu.com/p/31006686

  3. 洛谷P1970 花匠

    传送门 首先可以知道,如果一个序列是连续上升的,那么只需要取这一个序列中最高的元素即可,因为取其它的不能保证大于后面的.连续下降的序列同理.而这些恰好就是波峰和波谷. 所以遇到 $ j $ 比之前的 ...

  4. PHP: POST Content-Length of xxx bytes exceeds the limit of 8388608 bytes

    用户上传了 4 个附件,每个小于 5M,但是总大小超过了 15 M. 在 Nginx 日志中找到了如下错误信息,还没有到 Laravel 日志那一层. 2018/08/13 10:14:38 [err ...

  5. hdu3415 单调队列模板题

    比较裸的单调队列 先求前缀和,枚举所有结束位置1~n+k即可 #include<iostream> #include<cstdio> #include<cstring&g ...

  6. 性能测试十三:linux常用命令

    常用的linux命令: 目录类/ 根目录. 当前目录.. 上级目录cd / 进入根目录cd .. 进入上级目录ls 查看当前目录下的所有文件ll 查看当前目录下所有文件的详细信息pwd 显示当前目录的 ...

  7. python 全栈开发,Day14(列表推导式,生成器表达式,内置函数)

    一.列表生成式 生成1-100的列表 li = [] for i in range(1,101): li.append(i) print(li) 执行输出: [1,2,3...] 生成python1期 ...

  8. 步步为营-22-xml登录

    说明:通过xml完成用户登录信息的判断 1 搭建UI页面 2 新建一个student类,类中包含以上属性 public class Student { public int ID { get; set ...

  9. 步步为营-10-string的简单操作

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  10. C#4.0特性

    C# 4.0的主要主题是动态编程.对象的意义变得越来越“动态”,它们的结构和行为无法通过静态类型来捕获,或者至少编译器在编译程序时无法得知对象的结构和行为. a. 来自动态编程语言——如Python或 ...