Java Sort List of Objects with Comparable

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

Related Post: Java Sort List of Objects with Comparator

How to do this with Kotlin: Sort List of Objects with Comparable

I. Overview

1. Goal

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

2. Steps to do

– Implement Comparable interface for the class of objects you want to sort.
– Override compareTo(T other) 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 Collections.sort() method.

III. Practice

1. Create Class for objects to be sorted

2. Create test function

4. Run & check Result


Related Posts


Got Something To Say:

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

*