Chain methods in Comparator.comparing of List in Java 8 -


i have following objects

class trade{    private integer tradeid;    private event  eventid;    private integer tradeversion; }  class event{   private integer value; } 
  1. for above scenario trying write comparator.comparing(trade::geteventid().getvalue())
  2. the above did not work
  3. so went usual lambda following

    somelist.sort((trade o1, trade o2) -> o1.geteventid().getvalue().compareto(o2.geteventid().getvalue()));

and worked curious know if #1 possible?

  1. tried comparator.comparing(trade::geteventid).thencomparing(event::getvalue) no joy.
  2. note both of these classes third party cannot alter them.

it's easy :

trades.sort(comparator.comparingint(x -> x.geteventid().getvalue())); 

also notice version comparator.comparing(trade::geteventid).thencomparing(event::getvalue) semantically mean first sorting eventid value looks not want in first place.


Comments