我有一个未排序的列表,但我想以自定义方式进行排序,例如:
item_one_primary.pls
item_one_secondary.pls
item_one_last.pls
item_two_last.pls
item_two_primary.pls
item_two_secondary.pls
item_three_secondary.pls
item_three_last.pls
item_three_primary.pls
这是我的预定义顺序:
primary, secondary, last
应用排序后,上面无序列表应该是这样的:item_one_primary.pls
item_one_secondary.pls
item_one_last.pls
item_two_primary.pls
item_two_secondary.pls
item_two_last.pls
item_three_primary.pls
item_three_secondary.pls
item_three_last.pls
我尝试使用比较器,但最终得到了这样的结果:
item_one_primary.pls
item_two_primary.pls
item_three_primary.pls
有人知道如何解决这个问题吗?
以下是我使用的一些代码:
List<String> predefinedOrder;
public MyComparator(String[] predefinedOrder) {
this.predefinedOrder = Arrays.asList(predefinedOrder);
}
@Override
public int compare(String item1, String item2) {
return predefinedOrder.indexOf(item1) - predefinedOrder.indexOf(item2);
}
我没有包含拆分操作(首先按点(.)拆分,然后按下划线(_)拆分以获取预排序列表中的项)。