JAVA list按照另外一个给定list排序 多余的自动排在后面

public static void main(String[] args) {
   String[] regulation = {"诸葛亮","鲁班","xzcx","貂蝉","吕布"};
   final List regulationOrder = Arrays.asList(regulation);
   String[] ordered = {"nice","貂蝉","诸葛亮","xzcx","吕布","貂蝉","鲁班","诸葛亮","貂蝉","鲁班","诸葛亮","hahahahah","adsad"};
   List orderedList = Arrays.asList(ordered);
   Collections.sort(orderedList, new Comparator()
   {
      public int compare(String o1, String o2)
      {
         int io1 = regulationOrder.indexOf(o1);
         int io2 = regulationOrder.indexOf(o2);
         if(io1 == -1){
            return 1;
         }
         if(io2 == -1){
            return -1;
         }
         return io1 - io2;
      }
   });
   System.out.println(orderedList);
}

 

你可能感兴趣的:(工具类,基础,JA,list,o')