简体   繁体   English

无法在Java中使用自定义比较器

[英]Can't use custom Comparator in Java

I have problem with implementing my custom comparator for my class: 我在为类实现自定义比较器时遇到问题:

public class MyProject {
    private static class SuperClass {
        public SuperClass (ArrayList<Car> cars) {
            Collections.sort(cars);
            Collections.sort(cars, new Car.CustomOrder());
        }
    }

    public final static class Car implements Comparable<Car> {
        public Comparator<Car> customOrder() {
            return new CustomOrder();
        }

        public class CustomOrder implements Comparator<Car> {
            public int compare(Car c1, Car c2) {
                // some code
                return 1;
        }
    }
}

I would like to sort cars by custom comparator CustomOrder . 我想按自定义比较器CustomOrder对汽车进行排序。 I can't find my mistake so please help me. 我找不到我的错误,请帮助我。

  1. You have to implement public int compareTo(Car o) in Car class to compile your program. 您必须在Car类中实现public int compareTo(Car o)才能编译程序。

  2. Collections.sort(cars, new Car().new CustomOrder()); , because CustomOrder is an inner class, not a static nested one. ,因为CustomOrder是一个内部类,而不是静态的嵌套类。 It may be created by the existing reference to the outer class. 它可以通过对外部类的现有引用来创建。

An instance of an inner class can exist only within an instance of an outer class and has direct access to the methods and fields of its enclosing instance. 内部类的实例只能存在于外部类的实例中,并且可以直接访问其封闭实例的方法和字段。


It seems you don't understand inner and nested classes completely. 看来您不完全了解内部和嵌套类。 I suggest reading about them on Oracle Tutorials. 我建议在Oracle教程中阅读有关它们的内容。

As Andrew Tobilko suggested, you have to implement compareTo method in Car class because you want to compare Car objects. 如Andrew Tobilko所建议,您必须在Car类中实现compareTo方法,因为您想比较Car对象。

Take a look at the link Java Object Sorting 看一下链接Java Object Sorting

Also please, write clean code. 也请写干净的代码。 Your code is hard to read even though it is short. 即使代码很短,也很难阅读。

Try to get rid off the static keyword, that is not a nice way to go and can become easily in a bad design pattern. 尝试摆脱static关键字,这不是一个好方法,并且很容易以不良的设计模式出现。

you have the class Car, and a list of those somewhere, so independently of if the car implements or not a comparable, you can use an anonymous one and use the fields you need for the sort criteria... 您拥有Car类,并在某处列出了这些类,因此无论car实现与否,都可以使用匿名类,并使用排序条件所需的字段...

Example: 例:

sorting the list by OrderId (just an String) OrderId对列表进行排序(只是一个字符串)

public static void main(String[] args) {
    List<Car> myCars = new ArrayList<>();
    myCars.add(new Car(0, "qqweqw", "qwe"));
    myCars.add(new Car(1, "Aqqweqw", "qwe"));
    myCars.add(new Car(2, "Zqqweqw", "qwe"));

    System.out.println("unsorted list" + myCars);
    Collections.sort(myCars, new Comparator<Car>() {

        @Override
        public int compare(Car o1, Car o2) {
            return o1.getOrder().compareTo(o2.getOrder());
        }
    });
    System.out.println("sorted list" + myCars);

}

then as yo can see, no need to declare a new comparator class that binds dependencies to the Car class 然后您可以看到,无需声明将依赖项绑定到Car类的新比较器

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

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