Reordering a collection according to a related list of ids

后端 未结 6 1570
一向
一向 2021-01-16 06:52

I have a Collection (unordered) of objects with an id property, and an (ordered) List of ids. The id list is not sorted. I\'d like to crea

6条回答
  •  时光取名叫无心
    2021-01-16 07:29

    Sounds like you have a given order you want, that may or may not be numerically ascending / descending?

    I would recommend making your own Predicate below. org.apache.commons.collections.CollectionUtils.find(java.util.Collection collection, Predicate predicate);

    and loop over your specific ordering finding each actual object in the unordered list. N^2 solution

    and a creative use of java.util.collections.sort(List list, Comparator c), a org.apache.find(), and java.util.collections.swap(List list, int i, int j) you may get away from n^2

提交回复
热议问题