I have This method below which works for sorting out Student objects in an arraylist using String parameters, for example a Student Object would have a String name,int age and String course parameter
//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());
}
});
}
Now I want to implement this code with integer parameters
//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
}
});
}
But i get an error message saying Cannot invoke compareToIgnoreCase(int) on the primitive type int
How can i fix this code so that it will be able to sort the list with integer values?
Studentclass is of typeintthen just use a simple equality comparison operator.ints are notStrings.