0

Once one architect in my office told me that difference between comparator and comparable is; Comparator compares between two different class objects where comparable compares within the same class. I have one Class Trainee and another Student class. I want to sort names inside Student and Trainee class at the same time. Student has few names and Trainee has few names. Want to sort all these names in natural order. Is this possible to achieve? Below is my code with error.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Student {
String empName;

Student(String empName) {
    this.empName = empName;
}
}

 class Trainee {
 String studName;

 Trainee(String studName) {
    this.studName = studName;
 }
 }

public class Sort implements Comparator {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Student st = new Student("caroline");
    Student st1 = new Student("elley");
    Student st2 = new Student("fannah");
    Trainee tr = new Trainee("aundh");
    Trainee tr1 = new Trainee("Rohit");
    Trainee tr2 = new Trainee("Shammi");
    List list = new ArrayList<>();
    list.add(st);
    list.add(st1);
    list.add(st2);
    ArrayList list2 = new ArrayList<>();
    list2.add(tr);
    list2.add(tr1);
    list2.add(tr2);
    Collections.sort(Student, new Comparator<Trainee>() {
        @Override
        public int compare(Student s, Trainee t) {
            // TODO return 1 if rhs should be before lhs
            return s.empName.compareToIgnoreCase(t.studName);
        }

    });

    }

    }

2 Answers 2

1

There is a dirty way of doing that which results in a very unreadable code and shows a bad design

public class NameComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        Student s1 = o1 instanceof Student ? (Student)o1 : null;
        Student s2 = o2 instanceof Student ? (Student)o2 : null;
        Trainee t1 = o1 instanceof Trainee ? (Trainee)o1 : null;
        Trainee t2 = o2 instanceof Trainee ? (Trainee)o2 : null;
        String st1 = s1 != null ? s1.empName; t1 != null ? t1.studName; null;
        String st2 = s2 != null ? s2.empName; t2 != null ? t2.studName; null;
        return st1 != null ? st1.compareTo(st2): 0;
    }

}

The much better option is to use a common interface or object in your hierarchy and derive your Student and Trainee from that common object.

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

Comments

0

I doubt you need a comparator for this. Strings will sort in a natural order by default.

list = Collections.sort(list);
list2 = Collections.sort(list2);

should give you what you want.

The Comparator you have written makes no sense and will not compile. Both parameters must be of the same class. It is used to order a single list not lists in relation to each other.

this

Collections.sort(Student, new Comparator<Trainee>() {

should be

Collections.sort(list2 , new Comparator<Trainee>() {

That is you are saying sort my list of trainees using the comparison method I am giving you.

you don't sort the class Trainee you sort a collection of instances of Trainee.

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.