📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Kotlin - Sort ArrayList of Custom Objects By Property
package net.javaguides.kotlin.examples
import java.util.Arrays
fun main(args: Array < String > ) {
val list = ArrayList < User > ()
list.add(User("Tony", "Stark"))
list.add(User("Tom", "Cruise"))
list.add(User("John", "Cena"))
list.add(User("Ramesh", "Fadatare"))
var sortedList = list.sortedWith(compareBy({
it.firstName
}))
for (obj in sortedList) {
println(obj.firstName)
}
}
class User {
// Properties or Member Variables
var firstName: String;
var lastName: String;
// Secondary Constructor
constructor(firstName: String, lastName: String) {
this.firstName = firstName
this.lastName = lastName
}
}
John
Ramesh
Tom
Tony
Comments
Post a Comment
Leave Comment