用firebase排序数字 [英] sorting numbers with firebase
问题描述
我正在将每个游戏玩家的得分存储在Firebase中.
I'm storing a score for each player of my game in firebase.
scores {
name,
score
}
我想按照最高到最低的顺序对分数进行排序.
I want to sort the scores by highest to lowest.
这就是我正在尝试的
let highestScore = firebase.database().ref('scores').orderByChild('/score');
highestScore.on('value', getData, (err) => console.log(err));
这是建立数据库的方式
如何按最高到最低排序分数?
How can I sort the scores by highest to lowest?
谢谢.
此帖子被禁止提问:(我可以从中改变什么?
This post got be banned from asking questions :( what can I change from it?
推荐答案
Firebase数据库始终以升序返回结果.这意味着您需要在客户端代码中反转结果.使用当前结构,这意味着您可以将getData
实现为:
The Firebase Database always returns results in ascending order. That means you'll need to reverse the results in your client-side code. With your current structure, that means you can implement getData
as:
function getData(snapshot) {
var scores = [];
snapshot.forEach(function(childSnapshot) {
scores.unshift(childSnapshot.val());
});
console.log(scores);
}
This will show the scores in descending order, since Array.unshift
adds the item to the start of the array.
这篇关于用firebase排序数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!