Java Comparator字符排序(数字、字母、中文混合排序)
Java.lang.Character类 复习一下
这是修正前的排序效果:

这是修正后的排序效果:


完整示例:

以下是排序的部份代码(非全部代码:拼音首字母算法不在其中)
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Demo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String fileNames[] = { "fss01", "fss2", "fss01_22", "fss3", "fss1", "fss10", "fss20", "fss4", "fss30", "fss21", "fss12","fss01_3" };
- char chFileNames[][] = new char[fileNames.length][];
- String[] oldSortedNames = new String[fileNames.length];
- for (int i = 0; i < fileNames.length; i++) {
- chFileNames[i] = fileNames[i].toCharArray();
- oldSortedNames[i] = fileNames[i];
- }
- // Arrays.sort(fileNames, StrLogicCmp);
- Arrays.sort(chFileNames, ChsLogicCmp);
- System.out.println("_Random_" + "\t" + "_Tradion_" + "\t" + "_Target_");
- String line;
- for (int i = 0; i < fileNames.length; i++) {
- line = fileNames[i] + (fileNames[i].length() >= 8 ? "\t" : "\t\t");
- line += oldSortedNames[i] + (oldSortedNames[i].length() >= 8 ? "\t" : "\t\t");
- line += new String(chFileNames[i]);
- System.out.println(line);
- }
- }
- static Comparator<String> StrLogicCmp = new Comparator<String>() {
- @Override
- public int compare(String o1, String o2) {
- // TODO Auto-generated method stub
- return 0;
- }
- };
- // "f01s2s22", "f1s02s2"
- static Comparator<char[]> ChsLogicCmp = new Comparator<char[]>() {
- class Int{
- public int i;
- }
- public int findDigitEnd(char[] arrChar, Int at) {
- int k = at.i;
- char c = arrChar[k];
- boolean bFirstZero = (c == '0');
- while (k < arrChar.length) {
- c = arrChar[k];
- //first non-digit which is a high chance.
- if (c > '9' || c < '0') {
- break;
- }
- else if (bFirstZero && c == '0') {
- at.i++;
- }
- k++;
- }
- return k;
- }
- @Override
- public int compare(char[] a, char[] b) {
- if(a != null || b != null){
- Int aNonzeroIndex = new Int();
- Int bNonzeroIndex = new Int();
- int aIndex = 0, bIndex = 0,
- aComparedUnitTailIndex, bComparedUnitTailIndex;
- // Pattern pattern = Pattern.compile("D*(d+)D*");
- // Matcher matcher1 = pattern.matcher(a);
- // Matcher matcher2 = pattern.matcher(b);
- // if(matcher1.find() && matcher2.find()) {
- // String s1 = matcher1.group(1);
- // String s2 = matcher2.group(1);
- // }
- while(aIndex < a.length && bIndex < b.length){
- //aIndex <
- aNonzeroIndex.i = aIndex;
- bNonzeroIndex.i = bIndex;
- aComparedUnitTailIndex = findDigitEnd(a, aNonzeroIndex);
- bComparedUnitTailIndex = findDigitEnd(b, bNonzeroIndex);
- //compare by number
- if (aComparedUnitTailIndex > aIndex && bComparedUnitTailIndex > bIndex)
- {
- int aDigitIndex = aNonzeroIndex.i;
- int bDigitIndex = bNonzeroIndex.i;
- int aDigit = aComparedUnitTailIndex - aDigitIndex;
- int bDigit = bComparedUnitTailIndex - bDigitIndex;
- //compare by digit
- if(aDigit != bDigit)
- return aDigit - bDigit;
- //the number of their digit is same.
- while (aDigitIndex < aComparedUnitTailIndex){
- if (a[aDigitIndex] != b[bDigitIndex])
- return a[aDigitIndex] - b[bDigitIndex];
- aDigitIndex++;
- bDigitIndex++;
- }
- //if they are equal compared by number, compare the number of '0' when start with "0"
- //ps note: paNonZero and pbNonZero can be added the above loop "while", but it is changed meanwhile.
- //so, the following comparsion is ok.
- aDigit = aNonzeroIndex.i - aIndex;
- bDigit = bNonzeroIndex.i - bIndex;
- if (aDigit != bDigit)
- return aDigit - bDigit;
- aIndex = aComparedUnitTailIndex;
- bIndex = bComparedUnitTailIndex;
- }else{
- if (a[aIndex] != b[bIndex])
- return a[aIndex] - b[bIndex];
- aIndex++;
- bIndex++;
- }
- }
- }
- return a.length - b.length;
- }
- };
- }
Java Comparator字符排序(数字、字母、中文混合排序)的更多相关文章
- java只允许输入数字字母下划线中文
public static void main(String[] args) { Pattern pattern = Pattern.compile("[_0-9a-z]+"); ...
- MySQL、Oracle、DB2等数据库常规排序、自定义排序和按中文拼音字母排序
MySQL常规排序.自定义排序和按中文拼音字母排序,在实际的SQL编写时,我们有时候需要对条件集合进行排序. 下面给出3中比较常用的排序方式,mark一下 1.常规排序ASC DESC ASC 正序 ...
- JavaScript非数字(中文)排序
直接上代码: var arr=[ {name:"张散步",age:"23",sports:"篮球",number:"231123& ...
- Jtable 表格按多列排序(支持中文汉字排序)
这两天公司让做一个Jtable表格的排序,首先按A列排序,在A列相等时按B列排序,B列相等时按C列排序,ABC三列可以任意指定,最多分三列,这样的一个需求.由于我是大神,所以必须做了出来.ok,不自恋 ...
- Sql Server之ORDER BY不规则排序.如:中文月份排序
ORDER BY CASE Month WHEN '一月' THEN 1 WHEN '二月' THEN 2 WHEN '三月' THEN 3 WHEN '四月' THEN 4 WHEN '五月' TH ...
- js 混合排序(类似中文手机操作系统中的通讯录排序)
在阳光明媚最适合打盹的下午, 特意静音的手机竟然动起来了, 你没看错, 它震动了.... 上帝(顾客)来电, "报表查询系统左侧树状菜单中设备的中文名称不能排序", 要增加排序功能 ...
- mysql实现首字母从A-Z排序
1.常规排序ASC DESC ASC 正序 DESC倒叙 -- 此处不用多讲 2.自定义排序 自定义排序是根据自己想要的特定字符串(数字)顺序进行排序.主要是使用函数 FIELD(str,str1,s ...
- JS排序:localeCompare() 方法实现中文排序、sort方法实现数字英文混合排序
定义:用本地特定的顺序来比较两个字符串. 语法:stringObject.localeCompare(target) 参数:target——要以本地特定的顺序与 stringObject 进行比较的字 ...
- java 获取中文字符的首字母
原理: GB2312编码中的中文是按照拼音排序的 注意: 一些生僻的字无法获得正确的首字母,原因是这些字都是后加入的. import java.io.UnsupportedEncodingExcept ...
随机推荐
- Android动画分类
动画分类 View动画(补间动画).帧动画.属性动画 View动画(补间动画)包括:平移.旋转.缩放.透明度,View动画是一种渐近式动画 帧动画:图片切换动画 属性动画:通过动态改变对象的属性达到动 ...
- 从LeNet-5到DenseNet
一篇不错的总结:https://zhuanlan.zhihu.com/p/31006686
- 洛谷P1970 花匠
传送门 首先可以知道,如果一个序列是连续上升的,那么只需要取这一个序列中最高的元素即可,因为取其它的不能保证大于后面的.连续下降的序列同理.而这些恰好就是波峰和波谷. 所以遇到 $ j $ 比之前的 ...
- 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 ...
- hdu3415 单调队列模板题
比较裸的单调队列 先求前缀和,枚举所有结束位置1~n+k即可 #include<iostream> #include<cstdio> #include<cstring&g ...
- 性能测试十三:linux常用命令
常用的linux命令: 目录类/ 根目录. 当前目录.. 上级目录cd / 进入根目录cd .. 进入上级目录ls 查看当前目录下的所有文件ll 查看当前目录下所有文件的详细信息pwd 显示当前目录的 ...
- python 全栈开发,Day14(列表推导式,生成器表达式,内置函数)
一.列表生成式 生成1-100的列表 li = [] for i in range(1,101): li.append(i) print(li) 执行输出: [1,2,3...] 生成python1期 ...
- 步步为营-22-xml登录
说明:通过xml完成用户登录信息的判断 1 搭建UI页面 2 新建一个student类,类中包含以上属性 public class Student { public int ID { get; set ...
- 步步为营-10-string的简单操作
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- C#4.0特性
C# 4.0的主要主题是动态编程.对象的意义变得越来越“动态”,它们的结构和行为无法通过静态类型来捕获,或者至少编译器在编译程序时无法得知对象的结构和行为. a. 来自动态编程语言——如Python或 ...