I am giving app name, icon, package name, hour, mint, sec to an array my major problem is I want to sort this array according to most used app
appInfoList.add(AppInfo(appName, appIcon, packageName, hour, mint, sec))
appInfoList.sortBy { it.mint }
it didn't sort an array
CodePudding user response:
Is your appInfoList
a MutableList
? It needs to be a mutable list to sort the array. See this guide here on using Kotlin's sortBy
function.
You can cast your list to a mutable list before sorting, like this:
val mutableAppInfoList = appInfoList.toMutableList()
mutableAppInfoList.sortBy { it.mint }
The mutableAppInfoList
variable should contain the sorted list after calling sortBy
.
CodePudding user response:
Modify your code this way
appInfoList
.sortByDescending { it.hour * 3600 it.min * 60 it.sec }
The first element in the list will be the most used one.