In this kotlin tutorial example we will learn how to sort list of custom objects in kotlin. In the previous example we covered sort mutable list using sort,sorted and sortedDescending methods. To sort the custom object list we will use the following methods
sortedBy
sortedByDescending
sortWith
sortedWith
So let's create a Custom class Employee which has properties like name and salary.
This Employee class is a data class
data class Employee(var name:String,var salary:Float)
|
Sort custom list using sortedBy
while sort the list using sortedBy method return new list.
import java.util.* fun main() { val employees = listOf( Employee("Mallika", 5400f), Employee("Sachin", 30004f), Employee("Aish", 12000f), Employee("Namitha", 90000f)) employees.forEach { e-> println("${e.name} ${e.salary}") } print("After sort using sortedBy method") val sortedEmployee= employees.sortedBy { e->e.salary } sortedEmployee.forEach { e-> println("${e.name} ${e.salary}") } } data class Employee(var name:String,var salary:Float) |
Output:
Mallika 5400.0
Sachin 30004.0
Aish 12000.0
Namitha 90000.0
After sort using sortedBy methodMallika 5400.0
Aish 12000.0
Sachin 30004.0
Namitha 90000.0
|
Sort list by sortedByDescending
This will sort the given list by descending order based on our constraint
import java.util.* fun main() { val employees = listOf( Employee("Mallika", 5400f), Employee("Sachin", 30004f), Employee("Aish", 12000f), Employee("Namitha", 90000f)) employees.forEach { e-> println("${e.name} ${e.salary}") } print("After sort using sortedBy method") val sortedEmployee= employees.sortedByDescending { e->e.salary } sortedEmployee.forEach { e-> println("${e.name} ${e.salary}") } } data class Employee(var name:String,var salary:Float) |
Output:
Mallika 5400.0 |
Sort list of Custom objects by Compator
Till now we sort the list in ascending or descending order, by using Comparator we will sort the list of custom objects based on our own constraint (conditions).
So let's write a program to sort list of custom objects by length of the employee name
import java.util.* fun main() { val employees = listOf( Employee("Mallika", 5400f), Employee("Sachin", 30004f), Employee("Aish", 12000f), Employee("Namitha", 90000f)) employees.forEach { e-> println("${e.name} ${e.salary}") } println("After sort using sortedBy method") val sortedemployees = employees.sortedWith { student1, student2 -> student1.name.length - student2.name.length } sortedemployees.forEach { s -> println(s.name) } } data class Employee(var name:String,var salary:Float) |
Output:
Mallika 5400.0
Sachin 30004.0
Aish 12000.0
Namitha 90000.0
After sort using sortedBy method
Aish
Sachin
Mallika
Namitha
|
Sort the list based on Employee salary
import java.util.* fun main() { val employees = listOf( Employee("Mallika", 5400f), Employee("Sachin", 30004f), Employee("Aish", 12000f), Employee("Namitha", 90000f)) employees.forEach { e-> println("${e.name} ${e.salary}") } println("After sort using sortedBy method") val sortedemployees = employees.sortedWith { student1, student2 -> (student1.salary - student2.salary).toInt() } sortedemployees.forEach { s -> println("${s.name} : ${s.salary}") } } data class Employee(var name:String,var salary:Float) |
Output:
Mallika 5400.0
Sachin 30004.0
Aish 12000.0
Namitha 90000.0
After sort using sortedBy method
Mallika : 5400.0
Aish : 12000.0
Sachin : 30004.0
Namitha : 90000.0
|
Conclusion: In this example we covered sort list of custom objects using sortedBy(),SortedByDescending() and Comparator methods.
Article Contributed By :
|
|
|
|
786 Views |