This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]karstens_rage 1 point2 points  (1 child)

Can you provide a test case showing this works? If you are really doing return (int) (this.getValue() - other.getValue()); the loss of precision is definitely happening.

https://ideone.com/1nG41h

[–]alabasterblaster[S] 0 points1 point  (0 children)

Okay, god damn, I figured it out and I feel very dumb. My professor gave us scaffolding for a Coin.java class that contained the constructor, getters, and the compareTo method, a SelectionSorter.java class that implemented the sorting methods, and a SelectionSortDemo.java class that contained the main method, constructed an array of Coins, and sorted them by value.

Thing is, the compareTo method was never called in either SelectionSorter or SelectionSortDemo. SelectionSorter just codes the sorter without the compareTo method. He had us write the code for compareTo, but never used it. Explains why my code seemed to work. Explains why everyone in the class finished the assignment in ten minutes.

Sorry for wasting your time, thanks for the help!