1

I was trying a small code using Comparator to sort list of student through there roll number. But I am getting compile time error at this line:

Collections.sort(l);

Below my test code:

     package test;

     import java.util*;


      public class StudentUsingComparator implements
                    Comparator<StudentUsingComparator> {

        int roll;
        String name;

        public StudentUsingComparator(int roll, String name) {
                    this.name = name;
                    this.roll = roll;
        }
        @Override
        public int compare(StudentUsingComparator s1, StudentUsingComparator s2) {
                    return s1.roll - s2.roll;

        }

        public static void main(String[] args) {
                StudentUsingComparator student1 = new StudentUsingComparator(10, "ab");
                StudentUsingComparator student2 = new StudentUsingComparator(30, "cd");
               StudentUsingComparator student3 = new StudentUsingComparator(20, "bc");

              List<StudentUsingComparator> l = new ArrayList<StudentUsingComparator>();
               l.add(student1);
               l.add(student2);
               l.add(student3);
               System.out.println("unsorted collection is:  " + l);
               Collections.sort(l);   //m getting error in this line
               System.out.println("sorted collection is:  " + l);
        }
   }

Error message says:

bound mismatch: the generic method sort(List) of type collection is not applicable for the arguments list(). the inferred type StudentUsingComparator is not a valid substitute for the bounded parameter>

2 Answers 2

2

You confused Comparable<> and Comparator<>. Classes that have an intrinsic order should implement Comparable<>, then you can use Collecections.sort(l). When you use Collections.sort(l,c), c should implement Comparator<>, which defines an external ordering that is different and separate from the internal.

Sign up to request clarification or add additional context in comments.

Comments

0

Your code is mixing two concepts:

  1. You can define a Student class that implements Comparable, then you can use Collections.sort(l) to sort a List<Student> l.
  2. In addition to your class Student (could be Comparable or not) you define an extra class implementing Comparator<Student> and use Collections.sort(l,c) with a List<Student> l and a Comparator<Student> c.

While it is possible in theory to mix the two cases (like you did) and make Student implement Comparator<Student> (and then continue along the lines of 2.) this is a total no-go because either your objects have a natural order - then you should go with Comparable and case 1., or they don't, then defining the order should go to a separate Comparator-class.

Comments

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.