按对象参数排序对象的ArrayList - 整数

时间:2014-08-14 17:09:39

标签: java sorting arraylist

我有下面的方法,用于使用String参数对arraylist中的Student对象进行排序,例如,Student Object将具有String名称,int age和String course参数

//This Sorts with String parameters
public void sortArrayListByName()
{
    Collections.sort(sList,new Comparator<Student>() {
         public int compare(Student s1, Student s2) {
                 return s1.getName().compareTo(s2.getName());
         }
     });
}

现在我想用整数参数

实现这段代码
//This should sort the Arraylist of Objects
public void sortArrayListByAge()
{
    Collections.sort(sList,new Comparator<Student>() {
         public int compare(Student s1, Student s2) {
                 return s1.getAge().compareToIgnoreCase(s2.getAge()); //This Part gives me errors
         }
     });
}

但是我收到一条错误消息,说无法在原语类型int上调用compareToIgnoreCase(int)

如何修复此代码,以便能够使用整数值对列表进行排序?

5 个答案:

答案 0 :(得分:3)

您正在比较int值,它们是原始类型,int没有任何方法。这就是s1.getAge().compareToIgnoreCase(s2.getAge())失败的原因。

如果您碰巧使用Java 7或更早版本,请使用Integer#compare

public int compare(Student s1, Student s2) {
    return Integer.compare(s1.getAge(), s2.getAge()); 
}

否则,手动比较int变量(代码改编自Integer#compare来源,无需重新发明轮子):

public int compare(Student s1, Student s2) {
    return (s1.getAge() < s2.getAge()) ? -1 : ((s1.getAge() == s2.getAge()) ? 0 : 1); 
}

答案 1 :(得分:1)

您已经得到了答案,但其中一个选项是使用Java 8 Stream API中的sorted方法:

sList.stream().sorted((s1, s2) -> Integer.compare(s1.getAge(), s2.getAge()))
              .forEach(s -> System.out.println(s.getName() + ", " + s.getAge()));

答案 2 :(得分:0)

您可以使用常规比较运算符:

public void sortArrayListByAge()
{
    Collections.sort(sList,new Comparator<Student>() {
         public int compare(Student s1, Student s2) {
                 if (s1 != null && s2 != null) {
                    if (s1.getAge() < s2.getAge())
                        return -1;
                    else {
                        if (s1.getAge() > s2.getAge())
                            return 1;
                        else
                            return 0;
                    }
                 } else if (s1 != null) {
                    return 1; // s2 is null
                 } else if (s2 != null) {
                    return -1; // s1 is null
                 } else {
                    return 0; // both s1 and s2 are null
                 }
         }
     });

}

答案 3 :(得分:0)

试试这个:

//This should sort the Arraylist of Objects
public void sortArrayListByAge()
{
    Collections.sort(sList,new Comparator<Student>() {
         public int compare(Student s1, Student s2) {
                 int result = 0;   // assume equal
                 if ( (s1 == null && s2 != null )|| (s1.getAge() > s2.getAge()) ) {
                     result = 1;
                 }
                 if ( (s1 != null && s2 == null) || s1.getAge() < s2.getAge() ) {
                     result = -1;
                 }
                 return result;
         }
     });
}

答案 4 :(得分:0)

compareTocompareToIgnoreCase方法已被反转。

要对整数进行排序,例如年龄,请使用:

return s1.getAge().compareTo(s2.getAge());

要对字符串进行排序,如名称,并忽略大小写,请使用:

return s1.getName().compareToIgnoreCase(s2.getName());