My app has a top scores table, which uses .queryOrdered(byChild: "userHighScore") and .queryLimited(toLast: 100) to get the 100 highest scores:
func getTopScores() {
var scores = [String]()
self.reference
.child("users")
.queryOrdered(byChild: "userHighScore")
.queryLimited(toLast: 100)
.observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children.reversed() {
if let dataSnapshot = child as? DataSnapshot, let user = dataSnapshot.value as? [String:AnyObject] {
if let userHighScore = user["userHighScore"] as? Int {
var stringToAppend = "\(scores.count+1). "
if let userName = user["userName"] as? String {
stringToAppend += " \(userName) • \(userHighScore)"
}
scores.append(stringToAppend)
}
}
}
})
}
The problem is when there's a tie score.
Ideally, the first user to get that score would be above the others with the same score on the table (and the most recent user to get that score would be the lowest of those with the same score).
However, the table displays those with tied scores in the same random order that they appear in the Firebase Realtime Database.
Is there a way to sort tied scores chronologically?
I've spun my wheels on this for many hours now. Thanks in advance for any help!