Last active
August 26, 2020 23:25
-
-
Save alexpaul/6387f53f683b5b979b9aa73934db2377 to your computer and use it in GitHub Desktop.
Making sections for a 2D array to be used in a CollectionView or TableView. [Fellow] => [[Fellow]]
This file contains hidden or 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
// Making sections for a 2D array to be used in a CollectionView or TableView. [Fellow] => [[Fellow]] | |
import Foundation | |
struct Fellow { | |
let name: String | |
let cohort: String | |
static func allFellows() -> [Fellow] { | |
return [ | |
Fellow(name: "Kim", cohort: "data science"), | |
Fellow(name: "Herb", cohort: "data science"), | |
Fellow(name: "Sandy", cohort: "web"), | |
Fellow(name: "James", cohort: "web"), | |
Fellow(name: "Bob", cohort: "web"), | |
Fellow(name: "Al", cohort: "iOS"), | |
Fellow(name: "Cindy", cohort: "android"), | |
Fellow(name: "Yi", cohort: "android"), | |
Fellow(name: "Malik", cohort: "iOS"), | |
Fellow(name: "Nancy", cohort: "web"), | |
Fellow(name: "Xavier", cohort: "iOS"), | |
Fellow(name: "William", cohort: "android"), | |
] | |
} | |
} | |
extension Fellow { | |
static func makeSections(from fellows: [Fellow]) -> [[Fellow]] { | |
// 1 | |
// first make sure the collection is sorted using the section property you're interested in using e.g by season if working with a TV show model | |
let sortedFellows = fellows.sorted { $0.cohort < $1.cohort } // O(n log n) | |
// 2 | |
// get the first sorted cohort, this will be the section name | |
guard var currentCohort = sortedFellows.first?.cohort else { return [] } | |
// 3 | |
// make over the section name and get a unique collection using Set | |
let cohorts = Set(fellows.map { $0.cohort }) // O(n) | |
// 4 | |
// create an empty 2-D Array [[Fellow]], this will be the sections to be used in a table view or collection view's sections | |
var sections = Array(repeating: [Fellow](), count: cohorts.count) | |
// 5 | |
// declare a property currentIndex and assign it zero since we are starting to populate the array from index 0 | |
var currentIndex = 0 | |
// 6 | |
// iterate through the given array | |
for fellow in sortedFellows { // O(n) | |
// 7 | |
// if we are not looking athe the current section (cohort) then | |
if fellow.cohort != currentCohort { | |
// 8 | |
// increment the currentIndex so we are starting to populate a new section with the array | |
currentIndex += 1 | |
// 9 | |
// update the current section name (cohort) | |
currentCohort = fellow.cohort | |
} | |
// 10 | |
// append the current item (fellow) to the array withing the current section | |
sections[currentIndex].append(fellow) | |
} | |
// 11 | |
// we now have an array or arrays and are ready to populate our table view or collection view | |
return sections | |
} | |
} // complexity: O(n log n) if we have to sort the collection, if the collection is already sorted it will be O(n) | |
let allFellows = Fellow.allFellows() // [Fellow] - one dimentional array | |
let sections = Fellow.makeSections(from: allFellows) // [[Fellow]] - two-dimensional array | |
dump(sections) | |
/* | |
▿ 4 elements | |
▿ 3 elements | |
▿ __lldb_expr_27.Fellow | |
- name: "Cindy" | |
- cohort: "android" | |
▿ __lldb_expr_27.Fellow | |
- name: "Yi" | |
- cohort: "android" | |
▿ __lldb_expr_27.Fellow | |
- name: "William" | |
- cohort: "android" | |
▿ 2 elements | |
▿ __lldb_expr_27.Fellow | |
- name: "Kim" | |
- cohort: "data science" | |
▿ __lldb_expr_27.Fellow | |
- name: "Herb" | |
- cohort: "data science" | |
▿ 3 elements | |
▿ __lldb_expr_27.Fellow | |
- name: "Al" | |
- cohort: "iOS" | |
▿ __lldb_expr_27.Fellow | |
- name: "Malik" | |
- cohort: "iOS" | |
▿ __lldb_expr_27.Fellow | |
- name: "Xavier" | |
- cohort: "iOS" | |
▿ 4 elements | |
▿ __lldb_expr_27.Fellow | |
- name: "Sandy" | |
- cohort: "web" | |
▿ __lldb_expr_27.Fellow | |
- name: "James" | |
- cohort: "web" | |
▿ __lldb_expr_27.Fellow | |
- name: "Bob" | |
- cohort: "web" | |
▿ __lldb_expr_27.Fellow | |
- name: "Nancy" | |
- cohort: "web" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment