Skip to content

Instantly share code, notes, and snippets.

@ianhirschfeld
Last active June 23, 2017 20:21
Show Gist options
  • Save ianhirschfeld/fea31b8bbfbd59172a6a967e1392c86d to your computer and use it in GitHub Desktop.
Save ianhirschfeld/fea31b8bbfbd59172a6a967e1392c86d to your computer and use it in GitHub Desktop.
Example of some test data and how to sort it using an enum.
let MovieData = [
["title": "Jason Bourne", "cast": "Matt Damon, Alicia Vikander, Julia Stiles", "genre": "action"],
["title": "Suicide Squad", "cast": "Margot Robbie, Jared Leto, Will Smith", "genre": "action"],
["title": "Star Trek Beyond", "cast": "Chris Pine, Zachary Quinto, Zoe Saldana", "genre": "action"],
["title": "Deadpool", "cast": "Ryan Reynolds, Morena Baccarin, Gina Carano", "genre": "action"],
["title": "London Has Fallen", "cast": "Gerard Butler, Aaron Eckhart, Morgan Freeman, Angela Bassett", "genre": "action"],
["title": "Ghostbusters", "cast": "Kate McKinnon, Leslie Jones, Melissa McCarthy, Kristen Wiig", "genre": "comedy"],
["title": "Central Intelligence", "cast": "Dwayne Johnson, Kevin Hart", "genre": "comedy"],
["title": "Bad Moms", "cast": "Mila Kunis, Kristen Bell, Kathryn Hahn, Christina Applegate", "genre": "comedy"],
["title": "Keanu", "cast": "Jordan Peele, Keegan-Michael Key", "genre": "comedy"],
["title": "Neighbors 2: Sorority Rising", "cast": "Seth Rogen, Rose Byrne", "genre": "comedy"],
["title": "The Shallows", "cast": "Blake Lively", "genre": "drama"],
["title": "The Finest Hours", "cast": "Chris Pine, Casey Affleck, Holliday Grainger", "genre": "drama"],
["title": "10 Cloverfield Lane", "cast": "Mary Elizabeth Winstead, John Goodman, John Gallagher Jr.", "genre": "drama"],
["title": "A Hologram for the King", "cast": "Tom Hanks, Sarita Choudhury", "genre": "drama"],
["title": "Miracles from Heaven", "cast": "Jennifer Garner, Kylie Rogers, Martin Henderson", "genre": "drama"],
]
class ViewController: UIViewController {
// The magic enum to end our pain and suffering!
// For the most part the order of our cases do not matter.
// What is important is that our first case is set to 0, and that our last case is always `total`.
enum TableSection: Int {
case action = 0, comedy, drama, indie, total
}
// This is the size of our header sections that we will use later on.
let SectionHeaderHeight: CGFloat = 25
// Data variable to track our sorted data.
var data = [TableSection: [[String: String]]]()
// When generating sorted table data we can easily use our TableSection to make look up simple and easy to read.
func sortData() {
data[.action] = MovieData.filter({ $0["genre"] == "action" })
data[.comedy] = MovieData.filter({ $0["genre"] == "comedy" })
data[.drama] = MovieData.filter({ $0["genre"] == "drama" })
data[.indie] = MovieData.filter({ $0["genre"] == "indie" })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment