写一个Comparator
.
ComparatormyOrder = new Comparator () { public int compare(MyType a, MyType b) { return (b.booleanField() ? 1 : 0) - (a.booleanField() ? 1 : 0); } }
使用此比较器排序.
Collections.sort(myList, myOrder);
看到 Collections.sort
因此,您实际要求的似乎是将一个匹配元素移动到列表的前面.这应该很容易.
找到要移动的元素的索引:
int foundIndex = -1; for (int i = 0; i < tripList.size(); ++i) { if (tripList.get(i).freeCancellation) { foundIndex = i; break; } }
如果您找到这样的元素,并且它尚未在开头,请将其移至开头:
if (foundIndex > 0) { tripList.add(0, tripList.remove(foundIndex)); }