Chain methods in Comparator.comparing of List in Java 8

Viewed 3827

I have the following Objects

Class Trade{
   private Integer tradeId;
   private Event  eventId;
   private Integer tradeVersion;
}

Class Event{
  private Integer value;
}
  1. For the above scenario I am trying to write something like Comparator.comparing(Trade::getEventId().getValue())
  2. The above did not work
  3. So for now I went with the usual lambda which is the following

    someList.sort((Trade o1, Trade o2) -> o1.getEventId().getValue().compareTo(o2.getEventId().getValue()));

and it worked but curious to know if #1 is possible?

  1. Tried Comparator.comparing(Trade::getEventId).thenComparing(Event::getValue) but no joy.
  2. Note that both of these classes are third party so I cannot alter them.
2 Answers
Related