Firebase does not offer any way to reverse the sort order via a query. However, if you store your scores as negative values, then it's super easy.
Here's a structure
scores
score_0
amt: -20
score_1
amt: -10
score_2
amt: -12
score_3
amt: -1
score_4
amt: -22
and then the code that reads the top three 'high' scores of 22, followed by 20 and 12
let scoresRef = self.ref.child("scores")
let queryRef = scoresRef.queryOrdered(byChild: "amt").queryLimited(toFirst: 3)
queryRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
print(snap.value)
}
})
and the output
Optional({
amt = "-22";
})
Optional({
amt = "-20";
})
Optional({
amt = "-12";
})
and of course, if there's a limited number of scores, they could be read into an array and sorted as well.
For completeness and if you don't want to store scores as negative values, here's how that would work.
let scoresRef = self.ref.child("scores")
let queryRef = scoresRef.queryOrdered(byChild: "amt").queryLimited(toLast: 3)
queryRef.observeSingleEvent(of: .value, with: { snapshot in
var scoresArray = [Int]()
for child in snapshot.children {
let snap = child as! DataSnapshot
let score = snap.childSnapshot(forPath: "amt")
scoresArray.append(score.value as! Int)
}
scoresArray = scoresArray.reversed()
print(scoresArray)
})
The above code reads in the three highest values via queryLimited(toLast) which are 12, 20 and 22 and populates an array. The array can be reversed which sorts descending 22, 20 and 12.
A final option is to read them in as 12, 22, 20 but insert each amt into the array at position 0. So 12 would be at index 0, then 22 would be at index 0 and 12 would be at index 1 etc.