含有大写数字的视频标题排序不理想,特转换后比较
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Compare {
public static String num = "零一二三四五六七八九十";
public static String numF = " 十百千万 亿";
public static String numReg = "[零一二三四五六七八九十]";
public static String numFReg = "[十百千万亿]";
//public static String numF[][] = {{"十","1"} , {"百" , "2"} , {"千" , "3"} , {""}};
public static int compare(String s1 , String s2){
int i = format(s1) - format(s2);
if(i == 0){
return s1.compareTo(s2);
}else {
return i;
}
}
public static int format(String s){
int mark = hasNum(s);
int value = 1;
int all = 0;
int last = 0;
int lastPow = 0;
if(mark >= 0){
while (s.length() > mark) {
//当前数字是否可以算出数据
value = num.indexOf(s.charAt(mark));
if(value >= 0 && value != 10){
all += value;
if(mark !=0 && num.indexOf(s.charAt(mark -1)) != 10){
last = value;
}else {
last += value;
}
}else {
value = numF.indexOf(s.charAt(mark));
//当前的为numF
if(value > 0){
if(value >=4){
//万以上特殊处理
last = 0;
all += (int)(Math.pow(10, value) * (all - lastPow)) - (all - lastPow);
lastPow = all;
}else {
if(last > 0){
//前面是数字
all -= last;
last = (int)(last * Math.pow(10, value));
all += last;
}else {
last = (int)(Math.pow(10, value));;
all += last;
}
}
}
}
mark++;
}
}
return all;
}
public static int hasNum(String s) {
if(s != null){
Matcher matcher = Pattern.compile(numReg).matcher(s);
if(matcher.find())
{
return matcher.start();
}
else {
return -1;
}
}
else {
return -1;
}
}
public static void main(String[] args) {
System.out.println(format("十八亿三千万零九百六十四"));
System.out.println(format("九十九万六千七百八十三"));
}
}