Java 使用比较器对列表进行排序时出错
我正在尝试根据我的name属性对对象列表进行排序。所以我创建了一个比较器,我注意到它没有得到排序。如果在使用过程中出现任何错误,请告知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
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谢谢大家。理解这个概念。