On Thursday, November 08, 2012 10:05:43 AM [email protected] wrote: > > This is closer, but still doesn't work correctly if two "Misc" categories > are being compared, or one "Misc" category is compared to itself. > Try: > > @Override > public int compareTo(Category c) { > if(category.equals("Miscellaneous")) { > if(category.equals(c.category) return 0; // correctly > handle equality. > else return 1; > } elseif (c.category.equals("Miscellaneous")) { > return -1; > } else { > return category.compareTo(c.category); > } > }
You're right for the general case of compare(), but in my defense I'd like to say that merge sort will never compare the same object and the list was constrained to one special object, so in this case it'd work for the sort. But it's likely better to handle the general case "just in case" someone other than the sort uses the compare() so your comment is well taken. Nothing like fixing up one bad behaviour only to replace it with some more obscure one. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
