Loading...

commons-user@jakarta.apache.org

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: Sorting string array numerically matthew.hawthorne Fri Apr 30 09:01:45 2004

Søren Blidorf wrote:
I have a problem regarding sorting numerically.

I have the following pics:

pic_1, pic_2, pic_3, pic_4, pic_5, pic_6, pic_7, pic_8, pic_9, pic_10, pic_11, pic_12

But the sort makes a sort after first number then second number:

pic_1, pic_10, pic_11, pic_12, pic_2, pic_3, pic_4, pic_5, pic_6, pic_7, pic_8, pic_9

How can I make sure that the sort is numeric?

pic_1, pic_2, pic_3, pic_4, pic_5, pic_6, pic_7, pic_8, pic_9, pic_10, pic_11, pic_12


I'm not sure if this is the right list for this type of message...

But, you could write a custom Comparator, which parses the number from the string. Then you could use java.util.Arrays.asList() to convert your array to a List, use java.util.Collections.sort(List, Comparator) to get the results. I think this will work -- but it will probably be slower than just manually looping through the array and using a StringTokenizer to compare the strings yourself.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]