I have simple code:
struct User {
let id: Int
let name: String
}
var users: [User] = [User(id:1, name:"Alpha"), User(id:2, name:"Beta"), User(id:3, name:"Gamma")]
print(users)
users.sort { $0.name > $1.name }
print(users)
What is the best way of changing the sorting field based on variable? I mean, I want to have some variable like "sortBy" which contains value of sorting field ("id", "name" etc, struct may contain dozens of fields). And I can't find out the proper way of doing so in Swift.
Pseudo-code:
var sortBy = "name"
users.sort { $0.{sortBy} > $1.{sortBy} }