如何按值的降序从firebase获取数据? [英] How to get data from firebase in descending order of value?

查看:19
本文介绍了如何按值的降序从firebase获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得前 10 名用户的游戏最高分,并将它们显示在表格视图中.

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

在表格视图中,我希望显示得分最高的 10 人的姓名和分数.

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

任何帮助将不胜感激.谢谢

any help would be appreciated. Thanks

推荐答案

Firebase 不提供任何通过查询反转排序顺序的方法.但是,如果您将分数存储为负值,则非常容易.

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.

这是一个结构

scores
  score_0
     amt: -20
  score_1
     amt: -10
  score_2
     amt: -12
  score_3
     amt: -1
  score_4
     amt: -22

然后是读取前三个高"分数 22 的代码,然后是 20 和 12

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)
        }
    })

和输出

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)
    })

以上代码通过 queryLimited(toLast) 读取三个最高值,即 12、20 和 22,并填充一个数组.数组可以颠倒,按 22、20 和 12 降序排序.

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.

最后一个选项是将它们读入 12、22、20,但将每个 amt 插入数组的位置 0.所以 12 将位于索引 0,然后 22 将位于索引 0,12 将位于索引 1等

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.

这篇关于如何按值的降序从firebase获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆