public class SortString {
static String arr[] = {
"Now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "to", "the", "aid", "of", "their", "country"
};
public static void main(String args[]) {
for(int j = 0; j < arr.length; j++) {
for(int i = j + 1; i < arr.length; i++) {
if(arr[i].compareToIgnoreCase(arr[j]) < 0) {
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
/*
if(arr[i].compareTo(arr[j]) < 0) {
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
*/
}
System.out.println(arr[j]);
}
}
}
输出结果为:
compareToIgnoreCase( )
方法忽略大小写输出的结果是
aid
all
come
country
for
good
is
men
Now
of
the
the
their
time
to
to
compare()方法不忽略大小写
输出的结果为
Now
aid
all
come
country
for
good
is
men
of
the
the
their
time
to
to