[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sorting a collection



Use the Arrays.sort(Object[], Comparator) method.

Write a Comparator having the compare method use String's 
compareToIgnoreCase method.

It should look something like this:
public class BALComparator implements Comparator{
public int compare(Object o1, Object o2){
  String s1 = (String) o1;
  String s2 = (String) o2;
  return s1.compareIgnoreCase(s2);
}
}

You call would look like this:
String [] sArr = new String[10];
Comparator comp = new BALComparator();
//populate
Arrays.sort(sArr, comp);

Of course this assumes that your array is of Strings. If you have some other 
object, change your Comparator appropriately.

Hope this helps.

BAL

>From: Wade Steele <wsteele3@csc.com>
>To: ajug-members@www.ajug.org
>Subject: sorting a collection
>Date: Wed, 03 Jul 2002 11:29:04 -0400
>
>                                       As you know, the sort() method will
>                                       sort arays like this
>
>                                       Acorns
>                                       Bees
>                                       Birds
>                                       Cows
>                                       apples
>                                       rocks
>
>                                       I need to sort my arrays like this
>
>                                       Acorns
>                                       apples
>                                       Bees
>                                       Birds
>                                       Cows
>                                       rocks
>
>                                       Any idea how I can do this?  I need
>                                       it to be completely case
>                                       insensitive.
>
>                                       Thanks,
>
>                                       Wade
>
>
>
>
>
>




_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com