java字母排序_Java Comparator字符排序(数字、字母、中文混合排序)

这段Java代码展示了如何自定义比较逻辑对包含数字的字符串数组进行排序。它首先将字符串转换为字符数组,然后使用Comparator进行比较,考虑数字部分的大小以及前导零的影响。代码实现了传统排序和逻辑排序两种方式,并输出了排序结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 

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 

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 StrLogicCmp = new Comparator() {

@Override

public int compare(String o1, String o2) {

// TODO Auto-generated method stub

return 0;

}

};

// "f01s2s22", "f1s02s2"

static Comparator ChsLogicCmp = new Comparator() {

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 

c = arrChar[k];

//first non-digit which is a high chance.

if (c > '9' || c 

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 

//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 

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;

}

};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值