Take the 2-minute tour ×

Are there convenience methods for sorting arrays and collections that support method chaining in Java or popular open source utility libraries such as Guava or Apache-Commons? Something that I could throw into a snippet like this:

private static final List<String> myStrings = collections.unmodifiableList(
        Arrays.asList(new String["b","a"])

Then myStrings would contain "a", and "b" (in that order).

I could write methods like this myself, but I don't want to re-invent the wheel if they are already available in the libraries that I use.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You might be looking for Ordering.sortedCopy in Guava, but that's fluent in the comparator, not the collection.

Something fluent on the list might appear in Guava's FluentIterable, probably coming out in Guava release 12.

share|improve this answer
I don't really care about the copy. Probably doesn't matter that it does it, but would be ok without it. Know of anything for arrays that calls Arrays.sort then return the array? –  Stephen Ostermiller Mar 6 '12 at 20:01
No, nothing. (Arrays and generics are still super awkward.) –  Louis Wasserman Mar 6 '12 at 20:42
Actually, though, if you're just planning to wrap it in an unmodifiable list, then Ordering.immutableSortedCopy is even more likely to be your friend. –  Louis Wasserman Mar 7 '12 at 3:09
add comment

Your Answer


