I am building an Android application in which I would like to implement the custom sorting of User
Let me explain the requirement in detail.
Let's assume that there are 15 users stored in the ArrayList of type User and each users will have separate role mapping. The users must be grouped by roles in the same order as below.
1.Mechanic
2.Plumber
3.Electrician
Also, within the role groups they should be sorted primarily based on unread message count and secondarily based on alphabetical order
Case 1: Without Unread messages
Based on role-based grouping and alphabetical sorting(without unread messages), the users should be displayed in the following way.(The '0' in the bracket indicates the unread messages count)
Aarav,Mechanic(0)
Dhruv,Mechanic(0)
Jason,Mechanic(0)
Pranav,Mechanic(0)
Zeplin,Mechanic(0)
Amit,Plumber(0)
Baskaran,Plumber(0)
Garry,Plumber(0)
Rakesh,Plumber(0)
Shekar,Plumber(0)
Balu,Electrician(0)
Ragunathan,Electrician(0)
Prem kumar,Electrician(0)
Saravanan,Electrician(0)
Thanigaivel,Electrician(0)
Case 2: With unread messages count
When there are users(within role) with unread chat counts, the list should prioritize them primarily in the list and the alphabetical order should be considered as secondary. It should typically look like below.
Zeplin,Mechanic(20)
Pranav,Mechanic(10)
Aarav,Mechanic(0)
Dhruv,Mechanic(0)
Jason,Mechanic(0)
Garry,Plumber(5)
Rakesh,Plumber(5)
Amit,Plumber(0)
Baskaran,Plumber(0)
Shekar,Plumber(0)
Prem kumar,Electrician(10)
Balu,Electrician(0)
Ragunathan,Electrician(0)
Saravanan,Electrician(0)
Thanigaivel,Electrician(0)
Please help me to accomplish this.
Below is what I have tried
fun getListWithGroupingAndSorting(){
val users = ArrayList<User>
// Writing logic to pull list of users from server and assigning to `users`
return ArrayList(users.sortedWith(compareByDescending<User> {
it.unreadMessageCount
}.thenBy {
it.firstName
}))
}
This sorts the list primarily based on unread message count and secondarily based on alphabetical order but unable to accomplish in the above mentioned way.