Skip to content

Instantly share code, notes, and snippets.

@ck3g
Created July 30, 2018 12:05
Show Gist options
  • Save ck3g/1dfdb14a444323e95aa0fd9d63e51aee to your computer and use it in GitHub Desktop.
Save ck3g/1dfdb14a444323e95aa0fd9d63e51aee to your computer and use it in GitHub Desktop.
Sort array elements by two values in Swift
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