https://grokonez.com/kotlin/kotlin-sort-list-objects-kotlin-comparator-example
Kotlin – Sort List of Objects with Kotlin Comparator Example
This tutorial shows you way to Sort List of Objects by implementing Comparator
example.
Related posts:
- Kotlin – Compare Objects with Comparable Example
- Kotlin – Sort List of Objects with Comparable
- Kotlin Sort Object List with Kotlin Selector function – crossinline selector: (T) -> R?
- Kotlin Comparator Binary Search for Object List
I. Technology
- Java 1.8
- Kotlin 1.1.2
II. Overview
1. Goal
Sort list of threeMyDate(year,month,day)
objects.2. Steps to do
- Implement
Comparator
interface for the class that you use for handling sorting. - Override
compare(object1: T, object2: T)
method and: - return zero if
object1
is equalobject2
- a negative number if
object1
is less thanobject2
- a positive number if
object1
is greater thanobject2
- Use
sortedWith(comparator: Comparator)
method that returns aList
.III. Practice
1. Create Class for objects to be sorted
package com.javasampleapproach.objcomparator
data class MyDate (val year: Int, val month: Int, val day: Int) {
}
More at:
https://grokonez.com/kotlin/kotlin-sort-list-objects-kotlin-comparator-example
Kotlin – Sort List of Objects with Kotlin Comparator Example
Top comments (0)