自定义一个List排序规则,然后对另一个List按照该规则排序

转载请注明:http://blog.csdn.net/u012854870/article/details/78052159

//这里的顺序,是我自己定义的一个List<String>
String[] regulation = {"诸葛亮","鲁班","貂蝉","吕布"};
final List<String> regulationOrder = Arrays.asList(regulation);
String[] ordered = {"貂蝉","诸葛亮","吕布","貂蝉","鲁班","诸葛亮","貂蝉","鲁班","诸葛亮"};
List<String> orderedList = Arrays.asList(ordered);
Collections.sort(orderedList, new Comparator<String>()
{
    public int compare(String o1, String o2)
    {
        int io1 = regulationOrder.indexOf(o1);
        int io2 = regulationOrder.indexOf(o2);
        return io1 - io2;
    }
});
System.out.println(orderedList);
展开阅读全文

没有更多推荐了,返回首页