commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Henri Yandell <bay...@generationjava.com>
Subject Re: Sorting string array numerically
Date Fri, 30 Apr 2004 22:20:22 GMT

http://www.osjava.org/genjava/multiproject/genjava-core contains a
NumericStringComparator which can do this for you.

It's quite a bit longer than the one below because it never tries to
create Numbers etc to prove things are a number.

Source is:

http://www.osjava.org/genjava/multiproject/gj-core/xref/com/generationjava/compare/NumericStringComparator.html

Hen

On Fri, 30 Apr 2004, [iso-8859-1] Jean-Franšois wrote:

> 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
>


---------------------------------------------------------------------
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