by barrkel · 12y ago · 0 comments
sorted(Comparator<? super T> comparator)
Eurgh. Why isn't this a projection to a comparable type (or better, a projection to a tuple of comparable types, for use with lexicographical ordering)?Say you have class Country { int population; int name; }. You want the names of the top 3 countries by population:
countries.orderByDesc(x -> x.population).limit(3).map(x -> x.name)
But instead, you must do something like: countries.sorted((x, y) -> Integer.compare(y.population, x.population).limit(3).map(x -> x.name)
It gets much uglier when you want a lexicographical ordering by several attributes. countries.sorted(by(x -> x.population)).limit(3).map(x -> x.name)
where `by` looks something like: <S, T> Comparator<T> by(Projection<S, T> projection);
interface Projection<S, T> {
Comparable<S> project(T value);
}It's still ugly, and would prevent e.g. a radix sort being used when projecting an integer as the sort key, or American flag sort for strings.
so one can write: ``` contries.sorted(comparingInt(Country::getPopulation)). ... ```