public static void main(String[] args) {
//字母是升序,数值倒序,E系列放最后或数值倒序
List<String> list1 = new ArrayList<>();
list1.add("Y79A");
list1.add("Y67A");
list1.add("Y79");
list1.add("Y55");
list1.add("Y83_EX");
list1.add("Y83A(4+64G)");
list1.add("Y75s(4+64G)");
list1.add("X21i A(6+64G)福州思扬");
list1.add("X21A(6+128G)福州新华都");
list1.add("X21A(6+128G)福州电");
list1.add("X9s");
list1.add("X9Plus");
list1.add("X9s L");
list1.add("X20");
list1.add("X21A(6+128G)福建电");
list1.add("Y93全网通(4+64G)杭州新世纪");
list1.add("X20Plus");
String temp;
for (int i = 0; i< list1.size() - 1; i++) {
for (int j = 0; j < list1.size() - i - 1; j++) {
String o1 = list1.get(j);
String o2 = list1.get(j + 1);
char[] chars1 = o1.toCharArray();
char[] chars2 = o2.toCharArray();
List<String> s1List = charToList(chars1);
List<String> s2List = charToList(chars2);
List<String> digit1 = isDigit(s1List);
List<String> digit2 = isDigit(s2List);
boolean isChange = isChange(digit1, digit2);
//返回ture o1 >= o2 false o1 < o2
if (isChange) {
temp = list1.get(j);
list1.set(j, list1.get(j + 1));
list1.set(j + 1, temp);
}
}
System.out.print("第" + (i + 1) + "次排序结果为:");
for (String s : list1) {
System.out.print(s + " ");
}
System.out.print("\n");
}
}
public static boolean isChange(List<String> digit1, List<String> digit2) {
//返回ture,说明第一个数大于第二个数 o1 >= o2 否则第一个数小于第二个数 false o1 < o2
boolean isChange = false;
for (int k = 0; k < digit1.size(); k++) {
String s1 = "";
if (k < digit1.size()) {
s1 = digit1.get(k);
}
String s2 = "";
if (k < digit2.size()) {
s2 = digit2.get(k);
}
int cr = s1.compareTo(s2);
if (cr == 0) {
//相等,接着比较下一个
continue;
} else {
//不等,看看是字母还是数字
if (isDigit1(s1) && isDigit1(s2)) {
//数字
BigDecimal b1 = new BigDecimal(s1);
BigDecimal b2 = new BigDecimal(s2);
cr = b1.compareTo(b2);
if (cr > 0) {
//1 > 2
isChange = false;
break;
} else {
isChange = true;
break;
}
} else {
//是字母,大小规则 :0~9<A~Z<a~z X < Y
if (cr > 0) {
isChange = true;
break;
} else {
isChange = false;
break;
}
}
}
}
return isChange;
}
public static List<String> charToList(char[] chars) {
List<String> l1 = new ArrayList<>();
for (char aChar : chars) {
String s1 = String.valueOf(aChar);
l1.add(s1);
}
return l1;
}
public static List<String> isDigit(List<String> strList) {
List<String> list = new ArrayList<>();
if (strList != null && strList.size() > 1) {
for (int i = 0; i < strList.size(); i++) {
String s2 = "";
String s3 = "";
if (i + 1 < strList.size()) {
s2 = strList.get(i + 1);
}
if (i + 2 < strList.size()) {
s3 = strList.get(i + 2);
}
String s1 = strList.get(i);
if (isDigit1(s1) && isDigit1(s2)) {
s1 = s1 + s2;
if (isDigit1(s3)) {
s1 = s1 + s3;
i++;
}
list.add(s1);
i++;
continue;
}
list.add(s1);
}
} else {
return strList;
}
return list;
}
public static boolean isDigit1(String s1) {
if ("".equals(s1) || s1 == null) {
return false;
}
if (!Character.isDigit(s1.charAt(0))) {
return false;
}
return true;
}
冒泡排序实现当是字母时升序排序,当是数字时,实现降序排序。
最新推荐文章于 2021-05-17 17:43:02 发布