简体   繁体   English

使用Java中的比较器进行自定义排序

[英]Custom sort using comparator in java

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);
        }

    });

    }

    }

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. 更好的选择是在层次结构中使用公共接口或对象,并从该公共对象派生学生和受训者。

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. 您不对培训生类进行排序,而是对培训生实例的集合进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM