I have an array of LeagueTeam I need to sort. A League team has the usual stuff, points, shotsFor, shotsAgainst, totalShots etc. And the League will have an array of LeagueSortOptions such as [.points, .shots, .shotsFor]. This would sort by points then total shots then by shots for.
The leagueSortOptions are created by the user when creating the league so could be in any order depending on the user's needs eg.
[.points, .shots, .shotsFor]
[.shots, .shotsFor]
[.shotsFor, .points]
I will probably need to add more sort options down the line so I feel some sort of recursion is needed rather than creating a function for every single permutation possible. But in just can't wrap my head around it, been going round in circles for hours now. Any help or a pointer in the right direction would be greatly appreciated, thanks :)
enum LeagueSortOptions: String, Codable {
case points = "Points", shots = "Shots", shotsFor = "Shots For"
}
struct LeagueTeam : Codable {
var name: String
var gamesPlayed: Int
var gamesWon: Int
var gamesLost: Int
var gamesDrawn: Int
var shots: Int
var points: Int
var shotsFor: Int
var shotsAgainst: Int
}
I currently use this code to sort by points and then by shotsFor, but this only accounts for one permutation, which will probably be the most common:
teams.sort { (team1, team2) -> Bool in
if team1.points == team2.points {
if team1.shots > team2.shots { return true } else { return false }
}
if team1.points > team2.points { return true } else { return false }
}