January 2018
Intermediate to advanced
434 pages
14h 1m
English
Now, let's follow these steps to sort based on multiple fields of an object:
class Student(val age:Int, val GPA: Double)
val studentA=Student(11,2.0)val studentB=Student(11,2.1)val studentC=Student(11,1.3)val studentD=Student(12,1.3)val studentsList=listOf<Student>(studentA,studentB,studentC,studentD)
val sortedList=studentsList.sortedWith(compareBy({it.age},{it.GPA}))
sortedList.forEach { println("age: ${it.age}, GPA: ${it.GPA} ")}//Output: age: 11, GPA: 1.3 age: 11, GPA: 2.0 age: 11, GPA: 2.1 age: 12, GPA: 1.3
Read now
Unlock full access