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 ...
随机推荐
- 大数据处理算法--Bloom Filter布隆过滤
1. Bloom-Filter算法简介 Bloom-Filter,即布隆过滤器,1970年由Bloom中提出.它可以用于检索一个元素是否在一个集合中. Bloom Filter(BF)是一种空间效率很 ...
- 基于Golang设计一套微服务架构[转]
article- @嘟嘟噜- May/26/2018 18:35:30 如何基于Golang设计一套微服务架构 微服务(Microservices),这个近几年我们经常听到.那么现在市面上的的微服 ...
- exe4j中"this executable was created with an evaluation version exe4j"的解决
在使用exe4j时,如果您的exe4j没有注册,在运行有exe4j转换的*.jar为*.exe的可执行文件时,会提示:"this executable was created with an ...
- [转]CentOS7 下安装svn
1. 安装 centos(我这里使用的是CentOS7)下yum命令即可方便的完成安装 $ sudo yum install subversion 测试安装是否成功: $ svnserve --ver ...
- cf 1041C双指针
比赛的时候想着用单调队列做... 打完发现其实就是个双指针 /* 构造双指针解决即可 */ #include<iostream> #include<cstring> #incl ...
- python接口自动化测试十:字典、字符串、json之间的简单处理
# 字典 t = { # 键:值 '': '', '': '' } # 字典 # dict key: value key是唯一的,无序的 h = { ...
- intellij idea svn 修改文件后,父文件夹也标注修改
svn文件修改后,默认只有当前文件更改而父文件没有标注,很不直观:查了一顿后,发现,可以设置: File—->settings—->version control—–>勾选show ...
- 20165203 《网络对抗技术》week1 Kali的安装与配置
20165203 <网络对抗技术>week1 Kali的安装与配置 本人感觉Kali可以做很多有意思的事情,下面是本人的Kali的安装过程. 安装过程 光盘映像文件的下载 登录官网,选择下 ...
- webservice之restlet实现
转自LifeBa,http://www.lifeba.org/arch/restlet_develop_application_component_2.html但有改动,主要改动有:1. 修改了web ...
- Nmap的详细使用
Nmap的详细使用 介绍常用参数选项主机发现端口扫描服务和版本探测操作系统探测性能优化防火墙/IDS 躲避和哄骗输出 (一)介绍 Nmap — 网络探测工具和安全/端口扫描器. Nmap (“Netw ...