English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

First, I'm assuming the names are all String objects.

You can store them in an array and then sort them with the java.util.Arrays.sort() method. You could also put them in a Collection implementation (such as ArrayList) and then use the Collections.sort() method. Both of these approaches won't deal with case-insensitive order, though. Those beginning with uppercase will appear before all those beginning with lower case. To handle this "special" sorting need, you need to provide a Comparator implementation.

Here is an example Comparator:

class CaseInsensitiveStringComparator implements java.util.Comparator{

/**
* Compares two String objects in a case-insensitive
* natural order.
* @param o1
* @param o2
* @return
* @see
*/
public int compare(Object o1, Object o2) {
String a = (String)o1;
String b = (String)o2;
int retVal = a.compareToIgnoreCase(b);
return retVal;
}

}

Both the Arrays and Collections classes have sort methods that take an additional Comparator parameter.

For example, here is a little snippet that uses the Comparator described above:

String a = "a";
String A = "A";
String b = "b";
String B = "B";
ArrayList list = new ArrayList();
list.add(b);
list.add(A);
list.add(B);
list.add(a);

Collections.sort(list, new CaseInsensitiveStringComparator());
System.out.println(list);

Using the above code results in output of
[A, a, B, b]
Using Collections.sort(list) would have resulted in output of
[A, B, a, b]

2006-10-03 16:42:50 · answer #1 · answered by vincentgl 5 · 0 1

Just put the names in a list and call the sort method.

2006-10-03 15:46:05 · answer #2 · answered by Will 6 · 0 0

use java.util package in that

go through this link
http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeSet.html

2006-10-03 18:11:59 · answer #3 · answered by H@ri 2 · 0 0

fedest.com, questions and answers