only ordering the class B elements (actually I create a new list with completely new A-objects... if you do not want that, this can still serve as a starting point):
val listOfAWithOrderedB = listOfA.map {
it.copy(bs = it.bs.sortedByDescending(B::date))
}
which leads to:
A(bs=[B(date=2019-09-25, id=2), B(date=2019-09-24, id=1), B(date=2019-09-23, id=3)], id=4)
A(bs=[B(date=2019-09-23, id=7), B(date=2019-09-22, id=6), B(date=2019-09-21, id=5)], id=8)
A(bs=[B(date=2019-09-29, id=11), B(date=2019-09-23, id=10), B(date=2019-09-19, id=9)], id=12)
ordering by all B-dates and keeping a reference to the actual A:
val bSortedByDateAndTheirA = listOfA.flatMap { anA ->
anA.bs.map {
it to anA
}
}
.sortedByDescending { (b) -> b.date }
which leads to a List<Pair<B, A>> (again... a possible starting point) as follows:
(B(date=2019-09-29, id=11), A(bs=[B(date=2019-09-19, id=9), B(date=2019-09-23, id=10), B(date=2019-09-29, id=11)], id=12))
(B(date=2019-09-25, id=2), A(bs=[B(date=2019-09-24, id=1), B(date=2019-09-25, id=2), B(date=2019-09-23, id=3)], id=4))
(B(date=2019-09-24, id=1), A(bs=[B(date=2019-09-24, id=1), B(date=2019-09-25, id=2), B(date=2019-09-23, id=3)], id=4))
(B(date=2019-09-23, id=3), A(bs=[B(date=2019-09-24, id=1), B(date=2019-09-25, id=2), B(date=2019-09-23, id=3)], id=4))
(B(date=2019-09-23, id=7), A(bs=[B(date=2019-09-21, id=5), B(date=2019-09-22, id=6), B(date=2019-09-23, id=7)], id=8))
(B(date=2019-09-23, id=10), A(bs=[B(date=2019-09-19, id=9), B(date=2019-09-23, id=10), B(date=2019-09-29, id=11)], id=12))
(B(date=2019-09-22, id=6), A(bs=[B(date=2019-09-21, id=5), B(date=2019-09-22, id=6), B(date=2019-09-23, id=7)], id=8))
(B(date=2019-09-21, id=5), A(bs=[B(date=2019-09-21, id=5), B(date=2019-09-22, id=6), B(date=2019-09-23, id=7)], id=8))
(B(date=2019-09-19, id=9), A(bs=[B(date=2019-09-19, id=9), B(date=2019-09-23, id=10), B(date=2019-09-29, id=11)], id=12))
ClassBordered by date with its correspondingClassAor do you want a list ofClassAwhere only theClassBare ordered by date for eachClassA... or in other words: what should happen if there are twoClassB-dates of anyClassA-object that are both before and after otherClassA-object-containedClassB-dates?