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. 大数据处理算法--Bloom Filter布隆过滤

    1. Bloom-Filter算法简介 Bloom-Filter,即布隆过滤器,1970年由Bloom中提出.它可以用于检索一个元素是否在一个集合中. Bloom Filter(BF)是一种空间效率很 ...

  2. 基于Golang设计一套微服务架构[转]

      article- @嘟嘟噜- May/26/2018 18:35:30 如何基于Golang设计一套微服务架构 微服务(Microservices),这个近几年我们经常听到.那么现在市面上的的微服 ...

  3. exe4j中"this executable was created with an evaluation version exe4j"的解决

    在使用exe4j时,如果您的exe4j没有注册,在运行有exe4j转换的*.jar为*.exe的可执行文件时,会提示:"this executable was created with an ...

  4. [转]CentOS7 下安装svn

    1. 安装 centos(我这里使用的是CentOS7)下yum命令即可方便的完成安装 $ sudo yum install subversion 测试安装是否成功: $ svnserve --ver ...

  5. cf 1041C双指针

    比赛的时候想着用单调队列做... 打完发现其实就是个双指针 /* 构造双指针解决即可 */ #include<iostream> #include<cstring> #incl ...

  6. python接口自动化测试十:字典、字符串、json之间的简单处理

    # 字典 t = { #   键:值     '': '',     '': ''     } # 字典 # dict      key: value   key是唯一的,无序的 h = {      ...

  7. intellij idea svn 修改文件后,父文件夹也标注修改

    svn文件修改后,默认只有当前文件更改而父文件没有标注,很不直观:查了一顿后,发现,可以设置: File—->settings—->version control—–>勾选show ...

  8. 20165203 《网络对抗技术》week1 Kali的安装与配置

    20165203 <网络对抗技术>week1 Kali的安装与配置 本人感觉Kali可以做很多有意思的事情,下面是本人的Kali的安装过程. 安装过程 光盘映像文件的下载 登录官网,选择下 ...

  9. webservice之restlet实现

    转自LifeBa,http://www.lifeba.org/arch/restlet_develop_application_component_2.html但有改动,主要改动有:1. 修改了web ...

  10. Nmap的详细使用

    Nmap的详细使用 介绍常用参数选项主机发现端口扫描服务和版本探测操作系统探测性能优化防火墙/IDS 躲避和哄骗输出 (一)介绍 Nmap — 网络探测工具和安全/端口扫描器. Nmap (“Netw ...