user3233289
user3233289

Reputation: 21

Java 8 sort multiple objects

   public class B {
              private String name;
              private String  value;

             //Setters and Get
       }        
       public class C {
             private String name;
             private String  value;
        //Setters and Get Methods
       }
        public class D {
             private String name;
             private String  value;
             //Setters and Get
        }
        public class A {
              private B b;
              private C c;
              private D d;
            // Setters and Get
         }
        public class  Example{
          List<A> a = new Array List<A>();
            //Lets assume a  will contain objects of class B, C and D
           a .sort( Comparator.comparing(A::getB().getName).thenComparing(A::getC().getName));

         }

Sort field from one pojo , then sort field by next pojo. Need to understand how to sort in this situation. Can we use Comparator.comparing ()in this case?

Upvotes: 1

Views: 69

Answers (1)

Mureinik
Mureinik

Reputation: 311338

You can't use method refences like that, but you could just use lambda expressions:

a.sort(Comparator.comparing((A x) -> x.getB().getName())
                 .thenComparing(x -> x.getC().getName()));

Upvotes: 2

Related Questions