I'm new with swift langage and I'm trying to code a function which permits to get the 6 most popular people on my Firebase Database.
I wrote an observer which place users in an array like that:
[[userID: numberOfFollowers], [userID: numberOfFollowers], ...]
I would like to keep only the 6 users with the highest number of followers.
Do you know if it is possible?
var people = [[String: Int]]()
func loadPeople() {
var REF_FOLLOWERS = Database.database().reference().child("followers")
REF_FOLLOWERS.observe(.value, with: { (snapshot: DataSnapshot!) in
print("Got snapshot")
if let dict = snapshot.value as? [String: Any] {
for (key, value) in dict {
var count = 1
if let array = value as? [String: Any] {
for _ in array {
count += 1
}
}
self.people.append([key : count])
self.tableView.reloadData()
}
}
})
}