In Java how do you sort one list based on another? -
i've seen several other questions similiar 1 haven't been able find resolves problem.
my use case this: user has list of items (lista). reorder items , want persist order (listb), however, due restrictions i'm unable persist order on backend have sort lista after retrieve it.
so basically, have 2 arraylists (lista , listb). 1 specific order lists should in (listb) , other has list of items (lista). want sort lista based on listb.
collections.sort(listb, new comparator<item>() { public int compare(item left, item right) { return integer.compare(lista.indexof(left), lista.indexof(right)); } }); this quite inefficient, though, , should create map<item, integer> lista lookup positions of items faster.
guava has ready-to-use comparator doing that: ordering.explicit()
Comments
Post a Comment