commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "matthew.hawthorne" <ma...@apache.org>
Subject Re: Sorting string array numerically
Date Fri, 30 Apr 2004 16:01:34 GMT
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message