简体   繁体   English

Java覆盖Arrays.sort()的compareTo方法

[英]Java overriding compareTo method for Arrays.sort()

I am trying to override the compareTo method to be able to use Arrays.sort() But when i do this it seems like it is not working. 我正在尝试重写compareTo方法以能够使用Arrays.sort(),但是当我这样做时,它似乎无法正常工作。

I dont get any error back but i have a tester that tests and gives an error back. 我没有收到任何错误,但我有一个测试仪可以测试并给出错误。 I am supposed to compare the 'totalsales' from the items being compared 我应该比较所比较商品的“总销售额”

Here is the compare to method comparing two ItemSales Items 这是比较两个项目销售项目的比较方法

    @Override
    public int compareTo(ItemSales item) {

        return Double.compare(item.totalSales, this.totalSales);
    }

Here is the tester testing this, ofcourse theres much more besides this, but everything else works even clone(). 这是测试人员对此进行测试的方法,当然除了此方法还有很多,但是其他所有方法甚至都可以使用clone()。

ItemSales item = new ItemSales();
ItemSales newItem = new ItemSales(10.0, 50, 50.0);
ItemSales secondItem = (ItemSales) newItem.clone(); 
ItemSales[] myItems = { secondItem, item, newItem };

ItemSales[] myItems = { secondItem, item, newItem };

Arrays.sort(myItems);
if ( myItems[0].getTotalSales() == 860.0 && myItems[1].getTotalSales() == 870.0) {
    System.out.println("ItemSalesDemo.main()             - sorted, this is correct");
} else {
    System.out.println("ItemSalesDemo.main()             - ");
    System.out.println("ItemSalesDemo.main()             ------------------------");
    System.out.println("ItemSalesDemo.main()             -         Error        -");
    System.out.println("ItemSalesDemo.main()             ------------------------");
    System.out.println("ItemSalesDemo.main()             - item and newItem should have been equal");
    System.out.println("ItemSalesDemo.main()             - ");
    System.exit(-1);
} 

As you may know i get the "item and newItem should have been equal" error 如您所知,我收到“项目和newItem应该相等”的错误

I think the code would work better if you changed it: 我认为如果更改代码,代码会更好:

@Override
public int compareTo(ItemSales item) {
    if (this.totalSales < item.totalSales) {
        return -1;
    }
    else if(this.totalSales > item.totalSales){
        return 1;
    }

    return 0;
}

The problem with your earlier Double.compare() was that you had the parameters in wrong order, it should have been compare(this, item) and not the other way around 早期Double.compare()的问题在于参数顺序错误,应该将其作为compare(this,item),而不是相反

I see two possible difficulties in the compareTo method: 我在compareTo方法中看到两个可能的困难:

    if (this.totalSales != item.totalSales) {
        return 0;
    }

Normally, the compareTo method should return 0 if the two objects are equal. 通常,如果两个对象相等,则compareTo方法应返回0 You often hear the phrase, "compareTo should be consistent with equals()", which means that equals() should return true when compareTo() returns 0 , and equals() should return false when compareTo() returns something other than 0 . 你经常会听到这样的话,“的compareTo应该与equals一致()”,这意味着equals()应该返回truecompareTo()返回0 ,和equals()应该返回falsecompareTo()返回比其他的东西0

As a general rule, compareTo should return -1 if this is less than the item (the other ItemSales you're comparing against). 作为一般规则, compareTo应该返回-1 ,如果this是小于item (其他ItemSales你对比较)。 compareTo should return 1 if 'this' is greater than the other item . 如果'this'大于另一个item compareTo应该返回1

The second problem is what looks like a potential infinite loop: 第二个问题是看起来像一个潜在的无限循环:

    return item.compareTo(this);

I like what you have commented out better: 我更喜欢您评论的内容:

    return Double.compare(item.totalSales, this.totalSales);

That's more what I would expect from a compare method. 这是我对比较方法的期望。

Change your code as 将您的代码更改为

 @Override
    public int compareTo(ItemSales item) {

        return Double.compare( this.totalSales ,item.totalSales);
    }

As per your if condition , you are excepting in ascending order. 根据您的if条件,您按升序排列。 So change the parameters in Double.compare. 因此,更改Double.compare中的参数。 As others told please read the Comparable spec first 如其他人所言,请先阅读可比规格

myItems[0].getTotalSales() == 860.0

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

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