Warm tip: This article is reproduced from serverfault.com, please click

How to get data from firebase in descending order of value?

发布于 2017-07-17 07:05:01

I want to get game high scores for the top 10 users and display them in a table view.

leaderboards
 CQVudhI2OjTfNbqWOWi2T1DTWKw2
 HIGH-SCORE: 23
 PLAYER-NAME: Russell
 REACTION-TIME: .9
 SWIPES: 12

 F9IbjpVSE8g5YN3PX0sdhSNGULJ3
 HIGH-SCORE: 29
 PLAYER-NAME: Clayton
 REACTION-TIME: .87
 SWIPES: 22

 HDIHCUSJDHCJHZKSHJXH
 HIGH-SCORE: 89
 PLAYER-NAME: Damien
 REACTION-TIME: .77
 SWIPES: 32

 jdhsjdbwkjsdkahnkdnk232j3j2
 HIGH-SCORE: 43
 PLAYER-NAME: Christopher
 REACTION-TIME: .99
 SWIPES: 32

In the table view I wish to splay the name and the score of the 10 highest scorers.

any help would be appreciated. Thanks

Questioner
russsell phillips
Viewed
0
405k 2020-01-30 22:05:59

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.