实现一个Comparator就可以了
代码如下:
import java.util.Arrays
import java.util.Comparator
public class ChineseSort {
public static void main(String[] args) {
String[] list = {
"丙方",
"乙方",
"甲方",
"辛方",
"戊方", }
System.out.println("排序前: " + Arrays.toString(list))
sort(list)
System.out.println("排序后: " + Arrays.toString(list))
}
public static void sort(String[] args) {
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return getIndex(o1) - getIndex(o2)
}
}
Arrays.sort(args, comparator)
}
private static int getIndex(String s) {
for (int i = 0 i < DEFAULT_ARRAY.length i++) {
if (s.equals(DEFAULT_ARRAY[i])) {
return i
}
}
throw new RuntimeException("输入文字错误")
}
private static final String[] DEFAULT_ARRAY = {
"甲方",
"乙方",
"丙方",
漏猜没 兆姿 "丁方",
返纳 "戊方",
"己方",
"庚方",
"辛方",
"壬方",
"癸方" }
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)