Here its my model looks like
struct ContactModel{
var alphabet: String
var users: [UserModel]
}
struct UserModel{
var firstname: String
var lastname: String
}
Using sort alphabetically sorting working fine here Is the code:
contactArray.sort{ $0.alphabet < $1.alphabet }
Now, I want to sort ascending or descending order on bases of UserModel firstname
I try with nested sorting but giving me different errors like
Comparable and Binary operator '<' cannot be applied to two '[UserModel]' operands
I search on StackOverflow regarding this but haven't find nested sorting and operands error solution.
I tried this way
let sortedStudents = contactArray.sorted { (lhs: ContactModel, rhs: ContactModel) -> Bool in
let users1 = lhs.users.sorted(by: {
$0.firstname.lowercased() < $1.firstname.lowercased()
})
let users2 = rhs.users.sorted(by: {
$0.firstname.lowercased() < $1.firstname.lowercased()
})
return users1 < users2
}
But its not working. The only problem here that I am not able to do nested sort here want to sort firstname of UserModel and the We have ContactModel Array
var contactArray = [ContactModel](){
didSet{
contactArray.sort{ $0.alphabet < $1.alphabet } //Simple Sort working fine.
let sortedStudents = contactArray.sorted { (lhs: ContactModel, rhs: ContactModel) -> Bool in
let users1 = lhs.users.sorted(by: {
$0.firstname.lowercased() < $1.firstname.lowercased()
})
let users2 = rhs.users.sorted(by: {
$0.firstname.lowercased() < $1.firstname.lowercased()
})
return users1 < users2
}
self.tableView.reloadData()
}
}
Array Contains this
func getAllContacts() -> [ContactModel]{
return [
ContactModel(alphabet: "B", users: [
UserModel(firstname: "Bhaswar", lastname: "Patel"),
UserModel(firstname: "Bunty", lastname: "Patel")
]),
ContactModel(alphabet: "N", users: [
UserModel(firstname: "Nilesh", lastname: "Darji")
]),
ContactModel(alphabet: "Y", users: [
UserModel(firstname: "Yogesh", lastname: "Patel"),
UserModel(firstname: "Yash", lastname: "Patel")
])
]
}
Results:
Alphabetic section sorting working fine need to sort users now using firstname for this Yash is comes first then Yogesh
Any suggestion appreciated. Thanks!

usersarray, not thecontactArray? Can you show your desired result?