سؤال

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.

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().

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

I see two possible difficulties in the compareTo method:

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

Normally, the compareTo method should return 0 if the two objects are equal. 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.

As a general rule, compareTo should return -1 if this is less than the item (the other ItemSales you're comparing against). compareTo should return 1 if 'this' is greater than the other item.

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. So change the parameters in Double.compare. As others told please read the Comparable spec first

myItems[0].getTotalSales() == 860.0
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top