例如: String str1 = "D1" ;
String str2 = "D11" ;
String str3 = "D12" ;
String str4 = "D2" ;
String str5 = "D3" ;
String str6 = "H" ;
String str7 = "FO" ;
如何得到想要的結果: D1,D2,D3,D11,D12,F0,H
時間有點趕,有沒有現成的解決方案,謝謝大家!
String str2 = "D11" ;
String str3 = "D12" ;
String str4 = "D2" ;
String str5 = "D3" ;
String str6 = "H" ;
String str7 = "FO" ;
如何得到想要的結果: D1,D2,D3,D11,D12,F0,H
時間有點趕,有沒有現成的解決方案,謝謝大家!
解决方案 »
- 紧急求救啊,JAVA代码如何实现把里程值换成坐标值 arcgis
- tomcat启动警告:Could not parse struts.locale setting, substituting default VM locale
- SSH项目视频
- 问一个struts2,日期转换的问题
- spring如何来管理jsf请高手指点
- 谁有jsp+servlet+javaBean做的 电子政务网站
- text类型字段的hibernate映射问题
- javaMail
- 老长时间上不来了, 终于上来, 高兴散分, 同时提问: 我在tomcat5下如何设立一个myapp这样的虚ni目录, 高分
- J2EE系统中,多个EntityBean,如何实现序列化?
- 为什么这段javascript无法执行
- EJB 调用多个EJB,都用了Hibernate,多个库时JTA事务错
String ss[] = {"D12","D2","D03","B","D11","D8","D12","H","F0"};
Arrays.sort(ss);
for(String s : ss){
System.out.print(s+",");
}out->B,D03,D11,D12,D12,D2,D8,F0,H,
"D1", "D11", "D12", "D13", "D2", "D3", "H", "F0", "h1", "h0", "f1",
"F2", "d9", "1"
};
// 假设所有情况都是字母在前,数字在后
Comparator<String> cmp = new Comparator<String>() { public int compare(String str1, String str2) { // 字母部分
String alphabet1 = str1.replaceAll("\\d+$", "");
String alphabet2 = str2.replaceAll("\\d+$", ""); // 如果不想区分大小写,否则compareTo
int cmpAlphabet = alphabet1.compareToIgnoreCase(alphabet2);
if (cmpAlphabet != 0) {
return cmpAlphabet;
} // 数字部分
String numeric1 = str1.replaceAll("^[a-zA-Z]+", "");
String numeric2 = str2.replaceAll("^[a-zA-Z]+", "");
if ("".equals(numeric1)) {
// 即使numeric2也是空串也无所谓,当然,如果比较的不是String(或其他immutable对象)则另当别论
return -1;
}
if ("".equals(numeric2)) {
return 1;
}
int num1 = Integer.parseInt(numeric1);
int num2 = Integer.parseInt(numeric1);
return num1 - num2;
}
};
Arrays.sort(array, cmp); System.out.println(Arrays.toString(array));
}
代码: String str1 = "..."; String str2 ... String strx = "..." 得到String str = "xxx,xxx,xxx,xxx,xxx";如果没有理解错的话,上面的代码,关键步骤已经给出。这里是授人以渔的地方。剩下的如果还不能理解,就是资质问题了。
如果要手把手把所有代码贴出,那么请先双手奉上您的工资。
DetectContentOfSampleContent [] detectContentOfSampleContentArr2 = sample.getDetectContentOfSampleContentes().toArray(detectContentOfSampleContentArr1);
Comparator<DetectContentOfSampleContent> cmp = new Comparator<DetectContentOfSampleContent>() { public int compare(DetectContentOfSampleContent str1, DetectContentOfSampleContent str2) { if(str1.getDetect_of_content_shortname()==null||"null".equals(str1.getDetect_of_content_shortname())){
str1.setDetect_of_content_shortname("") ;
}
if(str2.getDetect_of_content_shortname()==null||"null".equals(str2.getDetect_of_content_shortname())){
str2.setDetect_of_content_shortname("");
}
String alphabet1 = str1.getDetect_of_content_shortname().replaceAll("\\d+$", "");
String alphabet2 = str2.getDetect_of_content_shortname().replaceAll("\\d+$", "");
int cmpAlphabet = alphabet1.compareToIgnoreCase(alphabet2);
if (cmpAlphabet != 0) {
return cmpAlphabet;
} String numeric1 = str1.getDetect_of_content_shortname().replaceAll("^[a-zA-Z]+", "");
String numeric2 = str2.getDetect_of_content_shortname().replaceAll("^[a-zA-Z]+", "");
if ("".equals(numeric1)) {
return 1;
}
if ("".equals(numeric2)) {
return -1;
}
int num1 = Integer.parseInt(numeric1);
int num2 = Integer.parseInt(numeric2);
if(num1>num2){
return num1;
}
return num1 - num2 ;
}
};
不知道对大家有啥帮助没,可以讨论下