Android Firebase检索最高100分
这是我的firebase的屏幕截图: 我正在尝试检索firebase数据库中的最高100分 我正在使用此代码向firebase添加新节点:Android Firebase检索最高100分,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,这是我的firebase的屏幕截图: 我正在尝试检索firebase数据库中的最高100分 我正在使用此代码向firebase添加新节点: Map<String, String> post1 = new HashMap<String, String>(); post1.put("name",name); post1.put("score",score); myRef.push().setValue(post1); Fir
Map<String, String> post1 = new HashMap<String, String>();
post1.put("name",name);
post1.put("score",score);
myRef.push().setValue(post1);
Firebase查询始终按升序排列。因此,您需要得到最后一个,而不是前100个
Query queryRef = myRef.orderByChild("score").limitToLast(100);
然后,在客户端,您需要反转项目
或者,您可以将反向属性添加到项目invertdscore:-99
。如果你这样做了,你就可以按照倒数的分数排序,而不必反转数组
这种情况以前经常被讨论过。我强烈建议您学习以下内容:
- (就在昨天)
或者从firebase获得最高数量
one citiesRef.orderBy("name").limit(3);
two citiesRef.orderBy("name", "desc").limit(3);
three citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
four citiesRef.where("population", ">", 100000).orderBy("population");
我想知道这是否真的有效,因为问题中的分数是字符串而不是数字。它会不会像这样排序:“1”、“12”、“2”、“3”、“33”、“333”、“4”?该死的。。。。抓到安德烈了。显然,我有一个盲点,因为我发誓我检查了它们是否存储为数字。只有将分数存储为数字(或零填充字符串)时,上述方法才有效。最好将分数存储为数字而不是字符串。查看有关弗兰克答案的评论,了解更多信息。
one citiesRef.orderBy("name").limit(3);
two citiesRef.orderBy("name", "desc").limit(3);
three citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
four citiesRef.where("population", ">", 100000).orderBy("population");