Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用比较器对列表进行排序时出错_Java_Comparator - Fatal编程技术网

Java 使用比较器对列表进行排序时出错

Java 使用比较器对列表进行排序时出错,java,comparator,Java,Comparator,我正在尝试根据我的name属性对对象列表进行排序。所以我创建了一个比较器,我注意到它没有得到排序。如果在使用过程中出现任何错误,请告知 List<Country> countryList = new ArrayList<Country>(); Country country1 = new Country("TEST1","Washington"); Country country2 = new Country ("TEST10", New Delhi"); Country

我正在尝试根据我的name属性对对象列表进行排序。所以我创建了一个比较器,我注意到它没有得到排序。如果在使用过程中出现任何错误,请告知

List<Country> countryList = new ArrayList<Country>();
Country country1 = new Country("TEST1","Washington");
Country country2 = new Country ("TEST10", New Delhi");
Country country3= new Country ("TEST9", London");
countryList.add(country1);
countryList.add(country2);

Collections.sort(countryList,new Comparator<Country>() {
            public int compare(Country o1, Country o2) {
                return o1.getName().compareTo(o2.getName());
            }
});
预期是

    TEST1 : Washington
    TEST9 : London
    TEST10 : New Delhi

按字母顺序,TEST10在TEST9之前

按字母顺序,TEST10在TEST9之前

您试图在此处按名称排序。但是名称是TEST1、TEST10和TEST9。比较时,您将按字母顺序得到以下顺序:

TEST1
TEST10
TEST9
您可以尝试以下代码:

Collections.sort(countryList,new Comparator<Country>() {
                    public int compare(Country o1, Country o2) {
                        if (o1.getName().length() > o2.getName().length()) return 1;
                        if (o1.getName().length() < o2.getName().length()) return -1;

                        return o1.getName().compareTo(o2.getName());
                    }
        });
Collections.sort(countryList,newcomparator(){
公共整数比较(国家o1,国家o2){
if(o1.getName().length()>o2.getName().length())返回1;
if(o1.getName().length()
您试图在此处按名称排序。但是名称是TEST1、TEST10和TEST9。比较时,您将按字母顺序得到以下顺序:

TEST1
TEST10
TEST9
您可以尝试以下代码:

Collections.sort(countryList,new Comparator<Country>() {
                    public int compare(Country o1, Country o2) {
                        if (o1.getName().length() > o2.getName().length()) return 1;
                        if (o1.getName().length() < o2.getName().length()) return -1;

                        return o1.getName().compareTo(o2.getName());
                    }
        });
Collections.sort(countryList,newcomparator(){
公共整数比较(国家o1,国家o2){
if(o1.getName().length()>o2.getName().length())返回1;
if(o1.getName().length()
字符串
比较
使用
字典法
比较字符串,但您的逻辑需要比较字符串的长度

Collections.sort(countryList,new Comparator<Country>() {
            public int compare(Country o1, Country o2) {
                if (o1.getName().length() > o2.getName().length()) return 1;
                if (o1.getName().length() < o2.getName().length()) return -1;

                return o1.getName().compareTo(o2.getName());
            }
});
Collections.sort(countryList,newcomparator(){
公共整数比较(国家o1,国家o2){
if(o1.getName().length()>o2.getName().length())返回1;
if(o1.getName().length()
字符串
比较
使用
字典法
比较字符串,但您的逻辑需要比较字符串的长度

Collections.sort(countryList,new Comparator<Country>() {
            public int compare(Country o1, Country o2) {
                if (o1.getName().length() > o2.getName().length()) return 1;
                if (o1.getName().length() < o2.getName().length()) return -1;

                return o1.getName().compareTo(o2.getName());
            }
});
Collections.sort(countryList,newcomparator(){
公共整数比较(国家o1,国家o2){
if(o1.getName().length()>o2.getName().length())返回1;
if(o1.getName().length()
你的代码不对。请说明你所说的
getName()
是什么意思?是“TEST1”还是“Washington”提到的,您正在按字母顺序对列表进行排序
TEST10
排在
TEST9
之前,这是因为
1
排在
9
之前。您必须修改
compare()
方法的实现,以便按照您想要的方式对元素进行排序。它将逐个字符进行比较,因此它将先打印TEST10,然后打印Test9谢谢大家。理解这个概念。你的代码不对。请说明你所说的
getName()
是什么意思?是“TEST1”还是“Washington”提到的,您正在按字母顺序对列表进行排序
TEST10
排在
TEST9
之前,这是因为
1
排在
9
之前。您必须修改
compare()
方法的实现,以便按照您想要的方式对元素进行排序。它将逐个字符进行比较,因此它将先打印TEST10,然后打印Test9谢谢大家。理解这个概念。