2.32字符串排序
2.32.1题目:将字符串按照首字母排序,如果首字母相同则按照第二个字母排序……依次类推。
2.32.2 源程序
public class StringSort {
public static void main(String[] args) {
String temp = null;
String[] s = new String[5];
s[0] = "china".toLowerCase();
s[1] = "apple".toLowerCase();
s[2] = "MONEY".toLowerCase();
s[3] = "BOOk".toLowerCase();
s[4] = "yeah".toLowerCase();
/*
* for(int i=0; i
* if(s[i].compareToIgnoreCase(s[j]) > 0) { temp = s[i]; s[i] = s[j];
* s[j] = temp; } } }
*/
for (int i = 0; i < s.length; i++) {
for (int j = i + 1; j < s.length; j++) {
if (compare(s[i], s[j]) == false) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]);
}
}
static boolean compare(String s1, String s2) {
boolean result = true;
for (int i = 0; i < s1.length() && i < s2.length(); i++) {
if (s1.charAt(i) > s2.charAt(i)) {
result = false;
break;
} else if (s1.charAt(i) < s2.charAt(i)) {
result = true;
break;
} else {
if (s1.length() < s2.length()) {
result = true;
} else {
result = false;
}
}
}
return result;
}
}
2.32.3 运行结果:
apple
book
china
money
yeah