用firebase排序数字 [英] sorting numbers with firebase

查看:34
本文介绍了用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屋!

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