Created
July 30, 2018 12:05
-
-
Save ck3g/1dfdb14a444323e95aa0fd9d63e51aee to your computer and use it in GitHub Desktop.
Sort array elements by two values in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct FilmSeries { | |
var title: String | |
var season: Int? | |
var episode: Int? | |
} | |
var series = [ | |
FilmSeries(title: "3-1", season: 3, episode: 1), | |
FilmSeries(title: "3-2", season: 3, episode: 2), | |
FilmSeries(title: "3-3", season: 3, episode: 3), | |
FilmSeries(title: "1-2", season: 1, episode: 2), | |
FilmSeries(title: "1-1", season: 1, episode: 1), | |
FilmSeries(title: "1-3", season: 1, episode: 3), | |
FilmSeries(title: "2-3", season: 2, episode: 3), | |
FilmSeries(title: "2-2", season: 2, episode: 2), | |
FilmSeries(title: "2-1", season: 2, episode: 1) | |
] | |
let sortedSeries = series.sorted { | |
$0.season! == $1.season! ? $0.episode! < $1.episode! : $0.season! < $1.season! | |
} | |
sortedSeries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment