COMPARATOR VS COMPARABLE INTERFACE

Both Comparable & Comparator are interface in Java.
Comparator belong to java.util package.
Comparable belong to java.lang package.
Comparator interface have public int compare (Object o1, Object o2) method which will return positive, zero and negative value. 
If
     - First argument is greater in compare to second argument -- return positive. 
     - First argument is equal to second argument -- return zero.
     - First argument is less than Second argument -- return negative.
Comparable interface have public int compareTo(Object o1) method which will return positive, zero and negative value.
If
     - "this" reference object is greater than specified object -- return positive.
     - "this" reference object is equal to specified object -- return zero.
     - "this" reference object is less than to specified object -- return negative.
If we want to sort object by natural ordering then we have to implement Comparable.
String and Wrapper Classes implements Comparable.

Example of Comparable Implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.ArrayList;
import java.util.Collections;
/*
 * Sorting of object based on marks
 * @author BBS
 * */
class StudentClass implements Comparable<StudentClass>
    {
        String rollno;
        String fname;
        int marks;
        public StudentClass(String rollNo,String fname,int mark)
            {
                this.rollno=rollNo;
                this.fname=fname;
                this.marks=mark;
            }
        public int compareTo(StudentClass student1)
            {
                int difference=this.marks-student1.marks;
                return difference;
            }
        public String toString()
            {
                return "Student (" + "rollno= "+ rollno + ", Name=" +fname+ " , Marks= "+marks+")" ;
            }
    }
public class ComparableImplementation
    {
        public static void main(String[] args)
            {
                StudentClass student1=new StudentClass("1","BBS1",80);
                StudentClass student2=new StudentClass("2","BBS2",30);
                StudentClass student3=new StudentClass("3","BBS3",40);
                ArrayList<StudentClass> studentlist=new ArrayList<StudentClass>();
                studentlist.add(student1);
                studentlist.add(student2);
                studentlist.add(student3);
                System.out.println("StudentList before Sorting");
                System.out.println(studentlist);
                Collections.sort(studentlist);
                System.out.println("StudentList after Sorting");
                System.out.println(studentlist);
            }
    }

Output:

comperator and comperable

Example of Comparator Implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
45
46
47
48
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/*
 * sorting based on Roll Number
 * @author BBS
 * */
class StudentClassforComparator
    {
        int rollno;
        String fname;
        int marks;
        public StudentClassforComparator(int rollNo,String fname,int mark)
            {
                this.rollno=rollNo;
                this.fname=fname;
                this.marks=mark;
            }
        public String toString()
            {
                return "Student (" + "rollno= "+ rollno + ", Name=" +fname+ " , Marks= "+marks+")" ;
            }
    }
public class ComparatorImplementation implements Comparator<StudentClassforComparator>;
    {
        public int compare(StudentClassforComparator student1,StudentClassforComparator student2)
            {
                int difference=student1.rollno-student2.rollno;
                return difference;
            }
        public static void main(String[] args)
            {
                StudentClassforComparator student1=new StudentClassforComparator(12,"BBS1",80);
                StudentClassforComparator student2=new StudentClassforComparator(2,"BBS2",30);
                StudentClassforComparator student3=new StudentClassforComparator(300,"BBS3",30);
                ArrayList<StudentClassforComparator> studentlist=new ArrayList<StudentClassforComparator>();
                studentlist.add(student1);
                studentlist.add(student2);
                studentlist.add(student3);
                System.out.println("StudentList before Sorting");
                System.out.println(studentlist);
                Collections.sort(studentlist,new ComparatorImplementation());
                System.out.println("StudentList after Sorting");
                System.out.println(studentlist);
            }
    }

Output:

When to use Comparable and Comparator ?

If you don't have access to the class/Object which you want to sort then write your own class and implement Comparator.

If sorting is going to happen in same way every time based on only one parameter,then implment Comparable.

If customer want to sort object on different parameter, then implement Comparator.