saintjab
saintjab

Reputation: 1642

Kotlin sort list of list based on parameter in inner list

I have a list of list that I want to sort based on a date value in inner list. Here's example of my list:

ClassA(id: String, classb : List<ClassB>) and ClassB is ClassB(id: String, date: Date) so I have list of ClassA which also contain list of ClassB and I want to order/sort List based on descending order of dates in inner classB

Upvotes: 1

Views: 1393

Answers (2)

Roland
Roland

Reputation: 23242

Assuming the following:

val int = AtomicInteger(0)
data class A(val bs: List<B>, val id: Int = int.incrementAndGet())
data class B(val date: LocalDate, val id: Int = int.incrementAndGet())

and a list (listOfA) as follows:

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)
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)
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)
  1. 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)
    
  2. 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))
    

Upvotes: 1

Black mamba
Black mamba

Reputation: 472

you can use Comparator to use this... here the code sample that you can use

Comparator object

companion object : Comparator<MyDate> {

override fun compare(a: MyDate, b: MyDate): Int = when {
    a.year != b.year -> a.year - b.year
    a.month != b.month -> a.month - b.month
    else -> a.day - b.day
}}

date model class

data class MyDate (val year: Int, val month: Int, val day: Int) {}

and this is how you can perform sorting

fun main(args: Array<String>) {
val myDates = listOfdays
myDates.sortedWith(CompareObjects).forEach { 

    add sorted list
}}

Upvotes: 0

Related Questions