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

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

问题描述

 排行榜
CQVudhI2OjTfNbqWOWi2T1DTWKw2
高分:23
玩家名称:Russell
反应时间:.9
SWIPES:12

F9IbjpVSE8g5YN3PX0sdhSNGULJ3
高分:29
玩家名称:克莱顿
反应时间:.87
SWIPES:22

HDIHCUSJDHCJHZKSHJXH
高分:89
玩家名称:Damien
反应时间:.77
SWIPES:32

jdhsjdbwkjsdkahnkdnk232j3j2
高分:43
PLAYER- NAME:Christopher
REACTION-TIME:.99
SWIPES:32

我希望展示10名得分手的名字和得分。



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

解决方案

Firebase没有任何方法通过查询来反转排序顺序。但是,如果您将分数存储为负值,那就太容易了。



这里有一个结构

<$ p $ 分数
score_0
amt:-20
score_1
amt:-10
score_2
amt:-12
score_3
amt:-1
score_4
amt:-22



然后是代码22,然后是20和12的前三个高分数。 = self.ref.child(scores)
let queryRef = scoresRef.queryOrdered(byChild:amt)。queryLimited(toFirst:3)

queryRef.observeSingleEvent(。值:{snapshot in
for snapshot.children中的孩子{
let snap = child as!DataSnapshot
print(snap.value)
}
})

和输出

 可选({
amt =-22;
))
可选({
amt =-20;
})
可选({
amt =-12;})
})

当然,如果分数有限,读入一个数组并进行排序。



为了保持完整性,如果您不想将分数存储为负值,则可以这样做。



$ p $ 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]()
$
let snap = child as!DataSnapshot
let score = snap.childSnapshot(forPath:amt)
scoresArray.append(score.value as !int)
}

scoresArray = scoresArray.reversed()
print(scoresArray)
})
pre>

上面的代码通过queryLimited(toLast )是12,20和22,并填充一个数组。数组可以颠倒,按照降序22,20和12进行排序。

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


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

解决方案

Firebase does not 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.

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

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