commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Ring <...@jdns.org>
Subject Re: [lang] ComparatorChain redesign
Date Sat, 27 Aug 2011 19:04:51 GMT
Hey Henri,

On Fri, Aug 26, 2011 at 11:50 PM, Henri Yandell <flamefew@gmail.com> wrote:
> Thanks James.
>
> I wanted to pull the comparator classes over from Collections as I
> wanted to still be able to support them (and use internally as I
> already had ComparableComparator hidden away) without having to deal
> with all of Collections.
>
> Maybe the answer though is to ignore them in favour of Ordering, much
> the way we've long ignored the Date API in favour of Joda Time.
>
> It looks like:
>
> ComparableComparator = Ordering.natural
> ReverseComparator = Ordering.reverse
> NullComparator = Ordering.nullsFirst/nullsLast
> ComparatorChain = Ordering.from + Ordering.compound
> FixedOrderComparator = Ordering.explicit

I think one of the most useful methods is onResultOf, e.g.

class Person {
  public static final Function<Person, String> lastNameFunction = new
Function<Person, String>() {
    @Override public String apply(Person input) {
      return person.getLastName();
    }
  }

  ...
}

List<Person> people = ...;

Ordering<Person> lastNameOrdering =
Ordering.natural().reverse().onResultOf(lastNameFunction);
List<Person> sortedPeople = lastNameOrdering.immutableSortedCopy(people);

> Hen

Regards,
James

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message