Hello,
You have to write a Comparator which parses the string, extracts the number,
and the compares the numbers
Here is a litle example which should normally work :
public class StringNumberComparator
implements java.util.Comparator {
/**
* compare les 2 objects
*/
public int compare(Object obj1, Object obj2)
throws ClassCastException {
String str1 = ((String) obj1).substring(4);
String str2 = ((String)obj2).substring(4);
int nb1 = Integer.parseInt(str1);
int nb2 = Integer.parseInt(str2);
if ( nb1 == nb2 ) {
return 0;
} else if ( nb1 > nb2 ) {
return 1;
} else {
return -1:
}
}
}
Henrard Jean-François
FORMATECH NEW TECHNOLOGIES
Avenue G. Poelslaan 8-10
BRUXELLES 1160 BRUSSEL
Tel. +32 2 675 08 41
Fax. +32 2 675 08 43
Web Site : http://www.formatech.be
Email jean-francois@formatech.be
----- Original Message -----
From: "Søren Blidorf" <soren@nolas.dk>
To: <commons-user@jakarta.apache.org>
Sent: Friday, April 30, 2004 11:06 AM
Subject: Sorting string array numerically
> Hi
>
> 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
>
> BR
>
> Soren
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
|