字符串排序,排序规则为 先排序第一个数字 若数字相同,则比较第二个数字
import java.util.Arrays;
public class arrequlas {
public static void main(String[] args) {
// 将字符串排序,排序规则为 先排序第一个数字 若数字相同,则比较第二个数字
String[] str = { "11-3", "1-2", "3-5", "2-3", "2-4", "1-5", "3-9","3-11" };
System.out.println(Arrays.toString(str));
// 冒泡
for (int i = 0; i < str.length - 1; i++) {
for (int j = 0; j < str.length - i - 1; j++) {
if (isy(str[j], str[j + 1])) {
String b = str[j];
str[j] = str[j + 1];
str[j + 1] = b;
}
}
}
System.out.println(Arrays.toString(str));
}
public static boolean isy(String str1, String str2) {
// 如何判断str1与str2相比 较大
// System.out.println(str1+","+str2);
String[] arr1 = str1.split("-");
String[] arr2 = str2.split("-");
if (Integer.parseInt(arr1[0]) > Integer.parseInt(arr2[0])) {
return true;
} else if (Integer.parseInt(arr1[1]) < Integer.parseInt(arr2[0])) {
return false;
} else {
if (Integer.parseInt(arr1[1]) > Integer.parseInt(arr2[1])) {
return true;
} else {
return false;
}
}
}
}