tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Russ Kepler <r...@kepler-eng.com>
Subject Re: This is just plain ... odd.
Date Thu, 08 Nov 2012 16:29:03 GMT
On Thursday, November 08, 2012 10:05:43 AM DJohnson@desknetinc.com 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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message