Java 按对象参数对对象数组列表排序-整数
下面我有一个方法,它使用字符串参数在arraylist中对学生对象进行排序,例如,学生对象将具有字符串名称、int age和String course参数Java 按对象参数对对象数组列表排序-整数,java,sorting,arraylist,Java,Sorting,Arraylist,下面我有一个方法,它使用字符串参数在arraylist中对学生对象进行排序,例如,学生对象将具有字符串名称、int age和String course参数 //This Sorts with String parameters public void sortArrayListByName() { Collections.sort(sList,new Comparator<Student>() { public int compare(Student s1,
//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());
}
});
}
//这将使用字符串参数进行排序
public void sortArrayListByName()
{
Collections.sort(sList,newcomparator(){
公共整数比较(学生s1、学生s2){
返回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
}
});
}
//这应该对对象的Arraylist进行排序
public void sortArrayListByAge()
{
Collections.sort(sList,newcomparator(){
公共整数比较(学生s1、学生s2){
返回s1.getAge().compareToIgnoreCase(s2.getAge());//这部分给了我错误
}
});
}
但是我收到一条错误消息,告诉我无法调用原语类型int的compareToIgnoreCase(int)
如何修复此代码,使其能够使用整数值对列表进行排序?您正在比较
int
值,这些值是基本类型,int
没有任何方法。这就是为什么s1.getAge().compareToIgnoreCase(s2.getAge())
失败的原因
如果您碰巧使用Java 7或更早版本,请使用:
否则,手动比较int
变量(代码改编自Integer#compare
源代码,无需重新发明控制盘):
公共整数比较(学生s1、学生s2){
返回值(s1.getAge()
您正在比较int
值,这些值是基本类型,int
没有任何方法。这就是为什么s1.getAge().compareToIgnoreCase(s2.getAge())
失败的原因
如果您碰巧使用Java 7或更早版本,请使用:
否则,手动比较int
变量(代码改编自Integer#compare
源代码,无需重新发明控制盘):
公共整数比较(学生s1、学生s2){
返回值(s1.getAge()
您可以使用普通比较运算符:
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
}
}
});
public void sortArrayListByAge()
{
Collections.sort(sList,newcomparator(){
公共整数比较(学生s1、学生s2){
如果(s1!=null&&s2!=null){
if(s1.getAge()s2.getAge())
返回1;
其他的
返回0;
}
}else如果(s1!=null){
返回1;//s2为空
}如果(s2!=null),则为else{
return-1;//s1为空
}否则{
返回0;//s1和s2都为空
}
}
});
}您可以使用普通比较运算符:
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
}
}
});
public void sortArrayListByAge()
{
Collections.sort(sList,newcomparator(){
公共整数比较(学生s1、学生s2){
如果(s1!=null&&s2!=null){
if(s1.getAge()s2.getAge())
返回1;
其他的
返回0;
}
}else如果(s1!=null){
返回1;//s2为空
}如果(s2!=null),则为else{
return-1;//s1为空
}否则{
返回0;//s1和s2都为空
}
}
});
}试试这个:
//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;
}
});
}
//这应该对对象的Arraylist进行排序
public void sortArrayListByAge()
{
Collections.sort(sList,newcomparator(){
公共整数比较(学生s1、学生s2){
int result=0;//假定相等
if((s1==null&&s2!=null)| |(s1.getAge()>s2.getAge()){
结果=1;
}
if((s1!=null&&s2==null)| | s1.getAge()
试试这个:
//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;
}
});
}
//这应该对对象的Arraylist进行排序
public void sortArrayListByAge()
{
Collections.sort(sList,newcomparator(){
公共整数比较(学生s1、学生s2){
int result=0;//假定相等
if((s1==null&&s2!=null)| |(s1.getAge()>s2.getAge()){
结果=1;
}
if((s1!=null&&s2==null)| | s1.getAge()将compareTo
和compareTignoreCase
方法倒置
要对整数(如年龄)进行排序,请使用:
return s1.getAge().compareTo(s2.getAge());
要对字符串(如名称)进行排序并忽略大小写,请使用:
return s1.getName().compareToIgnoreCase(s2.getName());
compareTo
和compareTignoreCase
方法是反向的
要对整数(如年龄)进行排序,请使用:
return s1.getAge().compareTo(s2.getAge());
要对字符串(如名称)进行排序并忽略大小写,请使用:
return s1.getName().compareToIgnoreCase(s2.getName());
您已经得到了答案,但其中一个选项是使用Java 8流API中的sorted
方法:
sList.stream().sorted((s1, s2) -> Integer.compare(s1.getAge(), s2.getAge()))
.forEach(s -> System.out.println(s.getName() + ", " + s.getAge()));
您已经得到了答案,但其中一个选项是使用Java 8流API中的sorted
方法:
sList.stream().sorted((s1, s2) -> Integer.compare(s1.getAge(), s2.getAge()))
.forEach(s -> System.out.println(s.getName() + ", " + s.getAge()));
如果Student
类中的年龄为int
类型,则只需使用简单的相等比较运算符。int
s不是String
s。如果Student
类中的年龄为int