Java ArrayList contains custom object
Ngày đăng:
27/12/2021
Trả lời:
0
Lượt xem:
195
Kotlin Program to Sort ArrayList of Custom Objects By PropertyIn this program, you'll learn to sort an arraylist of custom object by their given property in Kotlin. Show Example: Sort ArrayList of Custom Objects By Propertyimport java.util.* fun main(args: ArrayWhen you run the program, the output will be: In the above program, we've defined a CustomObject class with a String property, customProperty. In the main() method, we've created an array list of custom objects list, initialized with 5 objects. For sorting the list with the property, we use list's sortedWith() method. The sortedWith() method takes a comparator compareBy that compares customProperty of each object and sorts it. The sorted list is then stored in the variable sortedList. Here's the equivalent Java code: Java program to sort an ArrayList of custom objects by property. |