如何在Java中正确地对不同数字的字符串排序?
请原谅我的英语 有了这个代码如何在Java中正确地对不同数字的字符串排序?,java,android,zip,Java,Android,Zip,请原谅我的英语 有了这个代码 Collections.sort(mEntries, new Comparator<ZipEntry>() { public int compare(ZipEntry a, ZipEntry b) { return a.getName().compareTo(b.getName()); } }); 但是,当有像这样具有相同数字的字符串时,它可以对其进行正确
Collections.sort(mEntries, new Comparator<ZipEntry>() {
public int compare(ZipEntry a, ZipEntry b) {
return a.getName().compareTo(b.getName());
}
});
但是,当有像这样具有相同数字的字符串时,它可以对其进行正确排序
从此处:hello01、hello03、hello02、hello11、hello10。
进入这个:hello01、hello02、hello03、hello10、hello11
你知道我怎样才能达到我想要的结果吗
hello1,hello2,hello3,hello10,hello11
谢谢。您当前使用的排序是自然(字典)的
字符串
排序,通过调用字符串
上的比较
派生
根据词典排序,“10”
位于“2”
之前
使用相同基数、以数字结尾的String
s所需的排序是数字排序,这是Number
s(算术)的自然排序
根据自然的Number
排序,10
将位于2
之后
您想要的是:
- 检索两个
s字符串
- 验证它们唯一的区别是最后一个数字(使用正则表达式)
- 如果没有,就按你已经做过的那样按字典顺序进行比较
- 相反,如果唯一的区别是最后一个数字,则检索最后一个数字并创建两个
s(通过正则表达式和Integer
idiom)Integer.parseInt
- 然后对那些
s调用Integer
方法并返回结果compareTo
字符串
排序,通过调用字符串
上的compareTo
导出
根据词典排序,“10”
位于“2”
之前
使用相同基数、以数字结尾的String
s所需的排序是数字排序,这是Number
s(算术)的自然排序
根据自然的Number
排序,10
将位于2
之后
您想要的是:
- 检索两个
s字符串
- 验证它们唯一的区别是最后一个数字(使用正则表达式)
- 如果没有,就按你已经做过的那样按字典顺序进行比较
- 相反,如果唯一的区别是最后一个数字,则检索最后一个数字并创建两个
s(通过正则表达式和Integer
idiom)Integer.parseInt
- 然后对那些
s调用Integer
方法并返回结果compareTo
Collections.sort(mEntries, new Comparator<ZipEntry>() {
public int compare(ZipEntry a, ZipEntry b) {
return String.format("%100s", a.getName()).compareTo(
String.format("%100s", b.getName()));
}
});
return null;
Collections.sort(mEntries,newcomparator(){
公共整数比较(ZipEntry a、ZipEntry b){
返回String.format(“%100s”,a.getName())。比较(
格式(“%100s”,b.getName());
}
});
返回null;
收养自
没关系,它与此配合使用
Collections.sort(mEntries, new Comparator<ZipEntry>() {
public int compare(ZipEntry a, ZipEntry b) {
return String.format("%100s", a.getName()).compareTo(
String.format("%100s", b.getName()));
}
});
return null;
Collections.sort(mEntries,newcomparator(){
公共整数比较(ZipEntry a、ZipEntry b){
返回String.format(“%100s”,a.getName())。比较(
格式(“%100s”,b.getName());
}
});
返回null;
收养自
这仍将根据字符串进行排序,但不会根据您的要求根据数字进行排序。您确认这可以修复您的需求吗?这仍然会根据字符串进行排序,但不会根据您的需求根据数字进行排序。您确认这可以满足您的要求吗?