23

Parent is a class which is inherited by Child. which is inherited by GrandChild. Each class contains List of the child class(i.e Parent contains List of Child and Child contains List of GrandChild). Each class contains 50 attributes(attrib1-atrib50). getChildList() returns the arrayList of objects of type Child getGrandChildList() returns the arrayList of objects of type GrandChild

Let resultSet be List of Parent

List<Parent> resultSet

Now I want to sort the list based on some attributes. For example if I want to sort resultSet based on two parent attributes(say Attribute 1 and attribute 2, I use this code.

Comparator<Parent> byFirst = (e1, e2) -> e2.getAttrib1().compareTo(e1.getAttrib1());
Comparator<Parent> bySecond = (e1, e2) -> e1.getAttrib2().compareTo(e2.getAttrib2());

Comparator<Parent> byThird = byFirst.thenComparing(bySecond);


List<Parent> sortedList = resultSet.stream().sorted(byThird).collect(Collectors.toList());

Now I want to sort the parentlist based on attribute 1 of Child class and attribute 1 of GrandChild class. How should I sort this.

SilverNak
  • 3,283
  • 4
  • 28
  • 44
Manu Joy
  • 1,739
  • 6
  • 18
  • 30
  • 1
    How do you sort a list of `Parent` by attribute of `Child` if there are many `Child` in every `Parent`? Which child from the list would you use to get the attribute to compare? – Misha Apr 30 '15 at 09:55
  • by getChildList() I get the access to Child and I will select a child attribute. – Manu Joy Apr 30 '15 at 10:17
  • 1
    So if a parent has 10 children that all have a different value for attr1, which one would you use? – Misha Apr 30 '15 at 10:18

1 Answers1

33

Use Comparator.comparing to make the comparators. Just figure out what you want to compare. It will looks something like this, except you will write whatever logic you want to use to extract the values to compare:

Comparator<Parent> byAttr1ofFirstChild = Comparator.comparing(
    parent -> parent.getChildren().get(0).getAttr1()
);

Comparator<Parent> byAttr1ofFirstGrandChild = Comparator.comparing(
    parent -> parent.getChildren().get(0).getGrandChildren().get(0).getAttr1()
);


List<Parent> sortedList = parents.stream()
    .sorted(byAttr1ofFirstChild.thenComparing(byAttr1ofFirstGrandChild))
    .collect(toList());

Comparator.comparing would also make the examples in your question much nicer (using static imports) :

Comparator<Parent> byFirst = comparing(Parent::getAttrib1, reverseOrder());
Comparator<Parent> bySecond = comparing(Parent::getAttrib2);
Misha
  • 27,433
  • 6
  • 62
  • 78
  • reverseOrder() :- I can't indentiy this method in my IDE – Manu Joy Apr 30 '15 at 10:41
  • 2
    It's `Comparator.reverseOrder()`, but it makes the code more concise to use static imports – Misha Apr 30 '15 at 10:42
  • 1
    or you can do `comparing(Parent::getAttrib1).reversed();`. – Alexis C. Apr 30 '15 at 10:49
  • 1
    @AlexisC. Sadly, `comparing(...).reversed()` only works well with key extractors that are in the form of a method reference. If you tried to do `comparing(parent->parent.getAttrib1()).reversed()`, compiler won't take it without a type hint on `parent` variable. On the other hand, `comparing(parent->parent.getAttrb1(), reverseOrder())` will work fine. – Misha Apr 30 '15 at 10:57
  • 1
    @Misha Yes I know. I expect this to be improved in future to make this kind of constructions working though :-) – Alexis C. Apr 30 '15 at 10:59