Home > Net >  Get value from sorting data in Kolin
Get value from sorting data in Kolin

Time:12-01

My task is to return ArrayList of type transactionsList

First I have to parse date in string and then stream it (ascending)

I know how to do that but sortedWith give back type Unit not Array.

 val cmp = compareBy<transactionsList> { LocalDate.parse(it.date, 
DateTimeFormatter.ofPattern("dd.MM.yyyy.")) }


        val sortedList: List<transactionsList> = ArrayList()

      acountTransactionList
          . sortedWith(cmp)
          .forEach(::println)

        return acountTransactionList

I cannot store data from that sort because it gives me type Unit.

CodePudding user response:

The following works as intended (the issue is that forEach() method returns Unit, not each object):

fun main() {
    val acountTransactionList: ArrayList<transactionsList> = arrayListOf(transactionsList("10.10.2010."),
        transactionsList("10.10.2000."),
        transactionsList("10.09.2010."),
        transactionsList("10.11.2010."),
        transactionsList("11.11.2010."),
        transactionsList("10.10.2001."))

    val cmp = compareBy<transactionsList> {
        LocalDate.parse(it.date, DateTimeFormatter.ofPattern("dd.MM.yyyy."))
    }

    val sortedList: List<transactionsList> = acountTransactionList.sortedWith(cmp)
    println(sortedList)
}

data class transactionsList(val date: String)
  • Related