Assuming I have
final Iterable<String> unsorted = asList("FOO", "BAR", "PREFA", "ZOO", "PREFZ", "PREFOO");
What can I do to transform this unsorted list into this:
[PREFZ, PREFA, BAR, FOO, PREFOO, ZOO]
(a list which begin with known values that must appears first (here “PREFA” and “PREFZ”) and the rest is alphabetically sorted)
I think there are some usefull classes in guava that can make the job (Ordering, Predicates…), but I have not yet found a solution…
You specifically mentioned guava; along with Sylvain M’s answer, here’s another way (more as an academic exercise and demonstration of guava’s flexibility than anything else)
So, in other words, sort on natural order of the
Integerreturned byList.indexOf(), then break ties with natural order of the object itself.Messy, perhaps, but fun.