写着完,顺便记录下:
package com.techbirds.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StringCompareUtils {
final static String[] str={"1sadsad","123aa","DSDF","dadf","asd","fdsa","asdf","fdsa","qwe1","123a","d1"};
static List<String> arrs=new ArrayList<String>();
static{
arrs=Arrays.asList(str);
}
/**
* 考虑大小写
* 排序结果:[123a, 123aa, 1sadsad, DSDF, asd, asdf, d1, dadf, fdsa, fdsa, qwe1]
* @param str1
* @param str2
* @return
*/
public static boolean compareStrSumOnCase(final String str1,final String str2){
return getSumByStr(str1)>getSumByStr(str2);
}
/**
* 考虑大小写
* 排序结果:xxxx
* @param str1
* @param str2
* @param index
* @return
*/
public static boolean compareStrOnCase(final String str1,final String str2,final int index){
boolean flag=false;
//说明str1的索引越界
if(index>=str1.length()){
return false;
}
//说明str2的索引越界
if(index>=str2.length()){
return true;
}
if(str1.charAt(index)>str2.charAt(index)){
flag=true;
}else if(str1.charAt(index)==str2.charAt(index)) {
flag=compareStrOnCase(str1, str2, index+1);
}
return flag;
}
/**
* 计算得到字符串代表整数值,考虑大小写
* 例如:"ac",a=97 c=99
* ac=97*10+99
*
* @param str
* @return
*/
public static int getSumByStr(final String str){
int sum=0;
final int size=str.length();
for (int i = str.length(); i >0; i--) {
if(i-1==0){
sum+=str.charAt(size-i);
}else{
sum+=str.charAt(size-i)*10*(i-1);
}
}
return sum;
}
public static void main(String[] args) {
for (int i = 0; i < arrs.size(); i++) {
for (int j = i+1; j < arrs.size(); j++) {
if(compareStrOnCase(arrs.get(i), arrs.get(j),0)){
String tempStr=arrs.get(j);
arrs.set(j, arrs.get(i));
arrs.set(i, tempStr);
}
}
}
System.out.println(compareStrOnCase("123a", "d1", 0));
}
}