0

Here is the example code:

public class test {

    public static void main(String[] args) {
        ArrayList<Point> list = new ArrayList<Point>();
        list.add(new Point(1));
        list.add(new Point(4));
        list.add(new Point(2));
        list.add(new Point(3));
        list.add(new Point(2));
        Collections.sort(list);
    }

}

So I don't know why I can't use the Collections.sort until I add the compareTo method of Comparable interface to class Point.

I also do not know how the method compareTo sorting elements just by return -1, 0 and 1.

If anyone has any idea please explain to me.

Nam Trường
  • 13
  • 1
  • 5
  • 2
    _I also do not know how the method compareTo sorting elements just by return -1, 0 and 1._ This is explained in the javadoc for the [Comparable interface](https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html): _Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object._ – BackSlash Jul 28 '21 at 14:47
  • 1
    "I don't know why I can't use the Collections.sort until I add the compareTo method of Comparable interface to class Point." Well how do you expect them to be compared, and how would you expect Java to know that? – Jon Skeet Jul 28 '21 at 14:49
  • Take a look at https://stackoverflow.com/questions/14322585/collections-sort-implementation and https://stackoverflow.com/questions/12386075/how-does-compareto-and-compare-work – Slaw Jul 28 '21 at 14:58
  • @Jon Skeet then how can Java know if I want Java to sort a list of elements just by adding the method compareTo in the other class but don't even use/call the method in the main... – Nam Trường Jul 28 '21 at 15:40
  • @NamTrường: Because the sorting code *does* call it, to compare any two elements. Did you read the documentation for the `Collections.sort` method that you're calling? – Jon Skeet Jul 28 '21 at 15:47
  • @JonSkeet: I did read Java documentaries in my 1st language most of the time and it just mentions Collections.sort briefly. – Nam Trường Jul 28 '21 at 16:29
  • No, I'm talking about the documentation for Collections.sort specifically: https://docs.oracle.com/javase/10/docs/api/java/util/Collections.html#sort(java.util.List) – Jon Skeet Jul 28 '21 at 16:32

1 Answers1

0

So I don't know why I can't use the Collections.sort until I add the compareTo method of Comparable interface to class Point.

Because java has no idea why a new Point(1) should sort above or below a new Point(2).

I also do not know how the method compareTo sorting elements just by return -1, 0 and 1.

Let's say I give you a bag with a pieces of paper inside. Each paper lists a name and a phone number and I want you to write a phone book that is ordered, but you don't know which ordering.

All you can do is ask for any 2 notes which one is 'higher' than the other.

Then you can do the job: Let's say you have sorted 12 notes so far.

Then take the 13th note from the bag, and ask me if it is higher or lower than the 6th note in your pile. I say 'higher'. So you ask me about the 9th, I say higher. You ask about the 11th, I say lower. You ask me about the 10th, I say higher.

That's all you needed to know> You slide the note in between the 10th and 11th and move on to the 14th note.

See? An oracle that just answers the question: Given these 2 notes, tell me: Is this one lower than that one, higher, or the same? - And that's all you need.

You're also out of date, this code and the notion of writing your own compareTo is rather old. It's much simpler now:

list.sort(Comparator.comparingInt(Point::getValue));

is all you need. will sort by running p.getValue() on every point which produces an int value, and then sorting the points by treating a lower int as meaning that the point 'comes before' a point with a higher value.

You can then use secondary sorting orders (if 2 points have the same value, sort on their ID instead), define where null values should fit on the line, reverse the order, etcetera.

No need to mess with -1, 0, and +1.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72