I had posted a question last Friday regarding the use of Comparator and for my situation I learned that the Comparable interface was a better fit. However, I have another question and hoped that this group could help.
My understanding is that the COMPARETO method in the Comparable interface sorts based on the ASCII table order. Meaning that the numeric values would sort before the alpha values. My problem is that the list that we need to display on an intranet application needs to be sorted by ALPHA values first then by NUMERIC values. How can I accomplish this using the compareTo method? Here is the code that I have when I use the compareTo method that sorts almost like what I'm needing but falls a little short:
The results that I'm getting from the code above is as follows:
I need the results to be displayed as:
Any help or direction to get to the results that I need to be displayed would be greatly appreciated. Thanks.
[HENRY: Added Code Tags]
Post by:Henry Wong
My understanding is that the COMPARETO method in the Comparable interface sorts based on the ASCII table order.
The comparable for String, uses the ASCII value because that is how it is done. If you want it to do something else, then don't call the strings compare to method blindly. Add extra checks to see if the components are digits or characters -- if they are both the same type, then use the string comparator, if they are not, then return the answer. (it's a bit more complicated than that, but you should get the picture)