Collections.sort() using comparator? [closed]

不羁岁月 提交于 2019-12-01 08:05:46
Paul Samsotha

Collection.sort(l) assumes that the contents of l are Comparable. Collection.sort(1, Comparator) uses a custom comparator to compare the contents of l, this is what you did. The very idea of sorting (including the sort() method) implies the objects MUST be comparable - in this case, with either Comparable or Comparator.

Note that many Java objects are comparable already, including String, Date and Number. For those, you can just use Collection.sort(someList);

Example

Say you have a Circle class

public class Circle {
    double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getArea(){
        return radius * radius * Math.PI;
    }
}

If you created 100 Circle objects:

ArrayList<Circle> circleList = new ArrayList<>();

for (int i = 0; i < 100; i++) {
    // adds a circle with random radius
    circleList.add(new Circle((int)(Math.random() * 100)));
}

// try to sort the list
Collections.sort(circleList);   //compilation error: must be Comparable

You can't sort them because Java has no idea how to compare them. You have to tell this to Java:

public class Circle implements Comparable<Circle> {
    double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    // you MUST override the compareTo method from the Comparable interface
    @Override
    public int compareTo(Circle cirlce){
        if (this.getArea() > circle.getArea())
            return 1;
        else if (this.getArea() == circle.getArea())
            return 0;
        else 
            return -1;
    }

    public double getArea(){
        return radius * radius * Math.PI;
    }
}

With the compareTo() method in the Circle class, Java now knows how to compare them and can sort them.

Now you can do this:

Collections.sort(circleList);
// Yayyy I'm being sorted by the size of my areas!!!!!

Collections.sort which takes comparator sorts the List based on the Comparator provided by you and other follows natural sorting order.That is if you want to follow any custom sorting order then use this method. And nothing much to explain in that.

Use this link for explaination.

Sorting a collection is done using the Collections.sort(Collection) to sort your values. This method is for those who implements Comparable interface. This interface defines the method compare which performs pairwise comparison of the elements and returns -1 if the element is smaller then the compared element, 0 if it is equal and 1 if it is larger. A Common example is Integer class.

If what to sort differently you can define your own implementation based on the Comparatorinterface.This approach is that you then sort any object by any attribute or even a combination of attributes. For example if you have objects of type Person with an attribute income and dateOfBirth you could define different implementations of Comparator and sort the objects according to your needs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!