You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
|
|
public class Test {
|
|
|
|
public static void main(String[] args) {
|
|
String[] arr = new String[] { "0,1", "1,10", "0,100", "xx", "1,2" };
|
|
Arrays.sort(arr, new Comparator<String>() {
|
|
@Override
|
|
public int compare(String o1, String o2) {
|
|
if (o1.indexOf(",") == -1 || o2.indexOf(",") == -1) {
|
|
return 0;
|
|
}
|
|
String[] temp1 = o1.split(",");
|
|
String[] temp2 = o2.split(",");
|
|
if (temp1[0].equals(temp2[0])) {
|
|
return Integer.parseInt(temp1[1]) - Integer.parseInt(temp2[1]);
|
|
} else {
|
|
return Integer.parseInt(temp1[0]) - Integer.parseInt(temp2[0]);
|
|
}
|
|
}
|
|
});
|
|
System.out.println(Arrays.toString(arr));
|
|
List<String> list1 = new ArrayList<String>();
|
|
List<String> list2 = new ArrayList<String>();
|
|
List<String> list3 = new ArrayList<String>();
|
|
for (int i = 0; i < arr.length; i++) {
|
|
if (arr[i].indexOf(",") == -1 || "0".equals(arr[i].split(",")[0])) {
|
|
list1.add(arr[i]);
|
|
} else {
|
|
list2.add(arr[i]);
|
|
}
|
|
}
|
|
int maxLength = Math.max(list1.size(), list2.size());
|
|
for (int i = 0; i < maxLength; i++) {
|
|
if (i > list1.size() - 1) {
|
|
list3.add("");
|
|
list3.add(list2.get(i));
|
|
} else if (i > list2.size() - 1) {
|
|
list3.add(list1.get(i));
|
|
list3.add("");
|
|
} else {
|
|
list3.add(list1.get(i));
|
|
list3.add(list2.get(i));
|
|
}
|
|
}
|
|
System.out.println(Arrays.toString(list3.toArray()));
|
|
}
|
|
|
|
}
|