Collections自定义List排序规则
//这里的顺序,是我自己定义的一个List<String>
String[] regulation = {"jams","buke","rose","lua"};
final List<String> regulationOrder = Arrays.asList(regulation);
String[] ordered = {"rose","jams","lua","rose","buke","jams","rose","buke","jams"};
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);
原文地址:https://www.cnblogs.com/yoci/p/10239898.html
时间: 2024-10-13 22:47:03