1、题目:
java字符串排序
2、程序代码:
public static void mAIn(String[] args) { int N = 7; String temp; String[] strs = new String[] { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" }; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (!compare(strs[i], strs[j])) { temp = strs[i]; strs[i] = strs[j]; strs[j] = temp; } } } for (String str : strs) { System.out.println(str); } } public 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) || s1.length() < s2.length()) { result = true; break; } result = false; } return result; }