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:
Sort List of Objects with Kotlin Comparator Example
Sort List of Objects with Kotlin Comparable
Kotlin Binary Search for Object List with Comparison function – comparison: (T) -> Int
Kotlin Binary Search for Object List with Selector function – crossinline selector: (T) -> K?

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?)

– Method signature of sortedByDescending(crossinline selector: (T) -> R?)

So We need define an appropriate selector function for them.

-> Step to do:

  • Create data model
  • Create selector function
  • Do Sorting
1. Create data model

– Create Product model:

2. Create selector function

3. Do Sorting

III. Full Sourcecode

By JavaSampleApproach | December 15, 2017.

Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*