Kotlin – Sort List of Objects with Comparable

This tutorial shows you way to Sort List of Objects by Kotlin Comparable example.

Related posts:
Kotlin – Compare Objects with Comparable Example
Kotlin – Sort List of Objects with Kotlin Comparator Example
Kotlin Sort Object List with Kotlin Selector function – crossinline selector: (T) -> R?

I. Technology

– Java 1.8
– Kotlin 1.1.2

II. Overview

1. Goal

Sort list of three Date(year,month,day) objects.

2. Steps to do

– Implement Comparable interface for the class of objects you want to sort.
– Override compareTo(other: T) method and:
+ return zero if this object is equal other
+ a negative number if it’s less than other
+ a positive number if it’s greater than other
– Use sorted() method that returns a List.

III. Practice

1. Create Class for objects to be sorted

2. Create test function

3. Run & check Result

By grokonez | December 14, 2017.

Related Posts

Got Something To Say:

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