DEV Community

loizenai
loizenai

Posted on

Kotlin Sort Object List with Kotlin Selector function – crossinline selector: (T) -> R?

https://grokonez.com/kotlin/kotlin-sort-object-list-kotlin-selector-function-crossinline-selector-t-r

Kotlin Sort Object List with Kotlin Selector function – crossinline selector: (T) -> R?

In the tutorial, JavaSampleApproach will show you how to Sort Kotlin Object List with Kotlin Selector function - crossinline selector: (T) -> R?

Related posts:

I. Technologies

– Java 1.8
– Kotlin 1.1.61

II. Kotlin Sort Object List with Selector

We have sortedBy(crossinline selector: (T) -> R?) for ascending sorted list, and sortedByDescending(crossinline selector: (T) -> R?) for descending sorted list.

  • Method signature of sortedBy(crossinline selector: (T) -> R?)

public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List
  • Method signature of sortedByDescending(crossinline selector: (T) -> R?)

public inline fun > Iterable.sortedByDescending(crossinline selector: (T) -> R?): List

So We need define an appropriate selector function for them.

-> Step to do:

  • Create data model
  • Create selector function
  • Do Sorting

More at:

https://grokonez.com/kotlin/kotlin-sort-object-list-kotlin-selector-function-crossinline-selector-t-r

Kotlin Sort Object List with Kotlin Selector function – crossinline selector: (T) -> R?

Top comments (0)