如何在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
  • 验证它们唯一的区别是最后一个数字(使用正则表达式)
  • 如果没有,就按你已经做过的那样按字典顺序进行比较
  • 相反,如果唯一的区别是最后一个数字,则检索最后一个数字并创建两个
    Integer
    s(通过正则表达式和
    Integer.parseInt
    idiom)
  • 然后对那些
    Integer
    s调用
    compareTo
    方法并返回结果

您当前使用的排序是自然(词典)
字符串
排序,通过调用
字符串
上的
compareTo
导出

根据词典排序,
“10”
位于
“2”
之前

使用相同基数、以数字结尾的
String
s所需的排序是数字排序,这是
Number
s(算术)的自然排序

根据自然的
Number
排序,
10
将位于
2
之后

您想要的是:

  • 检索两个
    字符串
    s
  • 验证它们唯一的区别是最后一个数字(使用正则表达式)
  • 如果没有,就按你已经做过的那样按字典顺序进行比较
  • 相反,如果唯一的区别是最后一个数字,则检索最后一个数字并创建两个
    Integer
    s(通过正则表达式和
    Integer.parseInt
    idiom)
  • 然后对那些
    Integer
    s调用
    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;
收养自


这仍将根据字符串进行排序,但不会根据您的要求根据数字进行排序。您确认这可以修复您的需求吗?这仍然会根据字符串进行排序,但不会根据您的需求根据数字进行排序。您确认这可以满足您的要求吗?