Comparator with double type

前端 未结 10 1568
盖世英雄少女心
盖世英雄少女心 2020-11-28 11:50

I have written the following code:

public class NewClass2 implements Comparator
{
    public int compare(Point p1, Point p2)
    {
        retur         


        
10条回答
  •  执笔经年
    2020-11-28 12:16

    You don't need to return double.

    The Comparator interface is used to establish an ordering for the elements being compared. Having fields that use double is irrelevant to this ordering.

    Your code is fine.

    Sorry, I was wrong, reading the question again, this is what you need:

    public class NewClass2 implements Comparator {
        public int compare(Point p1, Point p2) {
            if (p1.getY() < p2.getY()) return -1;
            if (p1.getY() > p2.getY()) return 1;
            return 0;
        }    
    }
    

提交回复
热议问题