| |
9. 40. 4. 保持大小写字母在一起 |
|
import java.util.Arrays;
import java.util.Comparator;
public class MainClass {
public static void main(String[] args) {
String[] sa = new String[] { "adf", "fdsa", "ASDF", "FSA", "r", "R" };
System.out.println("Before sorting: " + Arrays.asList(sa));
Arrays.sort(sa, new AlphabeticComparator());
System.out.println("After sorting: " + Arrays.asList(sa));
}
}
class AlphabeticComparator implements Comparator {
public int compare(Object o1, Object o2) {
String s1 = (String) o1;
String s2 = (String) o2;
return s1.toLowerCase().compareTo(s2.toLowerCase());
}
}
/**/
|
|
Before sorting: [adf, fdsa, ASDF, FSA, r, R]
After sorting: [adf, ASDF, fdsa, FSA, r, R] |
|