您这这个程序是区分了大小写,先把大写按照字典排序,然后再排小写,如果按照字典那样,不区分大小写,应该怎样排序呢?
追答import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
String[] ary = {"test", "abc", "apple", "PEar", "AB"};
System.out.println("Before sorted, the array is: " + Arrays.toString(ary));
Arrays.sort(ary, new Comparator(){
public int compare(String o1, String o2) {
String[] temp = {o1.toLowerCase(), o2.toLowerCase()};
Arrays.sort(temp);
if(o1.equalsIgnoreCase(temp[0])){
return -1;
}else if(temp[0].equalsIgnoreCase(temp[1])){
return 0;
}else{
return 1;
}
}
});
System.out.println("After sorted, the new array is: " + Arrays.toString(ary));
}
}
-------testing
Before sorted, the array is: [test, abc, apple, PEar, AB]
After sorted, the new array is: [AB, abc, apple, PEar, test]