0

I am trying out Java Collections.sort method and get an error (please see in the code) which I don't understand. Java says, "The method sort(List, Comparator) in the type Collections is not applicable for the arguments (List, StudentComparator)". I don't know what I am doing wrong here.

I have a class called Students and it looks like this.

//import all necessary

public class Students implements Comparable<Students>{
public List<String> list;
public String firstname;
public String lastname;


public Students(){
    list = new ArrayList<String>();
}
public void addStudent(String firstname, String lastname){
    this.firstname = firstname;
    this.lastname = lastname;
    list.add(firstname + " " + lastname);
    Collections.sort(list); //This is fine though.
    Collections.sort(list, new StudentComparator()); //This gives me the error. I want to use this for custom comparison and in this case, by student's last name.

}

public String getFirstname() {
    return firstname;
}

public String getLastname() {
    return lastname;
}

@Override
public int compareTo(Students student) {
    return this.compareTo(student);
}


}

And I have another class that implements Comparator.

public class StudentComparator implements Comparator<Students>{

@Override
public int compare(Students student1, Students student2) {
    return student1.getLastname().compareTo(student2.getLastname());
}


}

I have no problem with using Anonymous Comparator interface and implement it right in the Students class, but when I create a new class that implements Comparator interface and try to sort the list, it gives me the error.

2
  • 1
    Try Collections.sort(list, (Comparator<Students>) (new StudentComparator())); Commented May 27, 2014 at 13:58
  • @DirkyJerky That won't work, you're still trying to sort a List<String> with a Comparator<Students>. Commented May 27, 2014 at 14:04

3 Answers 3

4

You're trying to sort a List<String> with a Comparator<Students>, which evidently cannot work. Perhaps you meant to use a List<Students> instead? Or a Comparator<String>?

Also, you have this:

@Override
public int compareTo(Students student) {
    return this.compareTo(student);
}

which will simply call itself indefinitely. I'm not fully sure what you wanted to do there; perhaps sort by the last name as you do in StudentComparator?

@Override
public int compareTo(Students student) {
    return getLastname().compareTo(student.getLastname());
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh yeah.. I was comparing Apple and Orange here. Thanks for the help!
0

I strongly recommend that you modify your Students bean and add the Comparable interface directly to it. Semantically speaking, Comparator works best when you need multiple sorting options. If you don't need multiple options for the same data type or if you want to provide a default sort then you should implement the Comparable interface. It's less code and you don't have to do the prep work to get it going.

mkyong has an excellent example of how Comparator and Comparable work: http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

1 Comment

It's not implemented correctly nor is it being used. Hence the recommendation and the link to a good tutorial.
0

So, I cleaned up the code. Noticed how I set each Students's attribute inside the constructor? This is very good practice for setting attributes of the objects. We needed import java.lang.Comparable; for

public int compareTo(Students student){ 
    return this.getLastName().compareTo(student.lastName);
}

and we needed import java.util.Comparator; for

@Override
public int compare(Students student1, Students student2){
    return student1.getLastName().compareTo(student2.getLastName());
}
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.lang.Comparable;


public class Students implements Comparable<Students>{

public List<Students> list = new ArrayList<Students>();
public String firstName;
public String lastName;

public Students(String firstName, String lastName){
    this.firstName = firstName;
    this.lastName = lastName;
}

public void addStudent(String firstName, String lastName){
    list.add(new Students(firstName,lastName));
    Collections.sort(list); // this is fine though
    Collections.sort(list, new StudentComparator()); // this gives me an error
}

public String getFirstName(){
    return firstName;
}

public String getLastName(){
    return lastName;
}

@Override 
/* Compare a given student lastName to the current Object
 *  if greater than the current object, return -1
 *  if equals to the current object, return 0
 *  if less than the current object, return 1
 *  Here is Comparable below:
 */
public int compareTo(Students student){ 
    return this.getLastName().compareTo(student.lastName);
}
}

and we need import java.util.Comparator; for

public class StudentComparator implements Comparator<Students>{

@Override
    public int compare(Students student1, Students student2){
        return student1.getLastName().compareTo(student2.getLastName());
    }
}

Hope that explains it.

1 Comment

Thanks for the help! I got it now. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.