gpt4 book ai didi

android - firebase orderByChild 只为一个 child 返回奇怪的顺序

转载 作者:搜寻专家 更新时间:2023-11-01 08:29:37 24 4
gpt4 key购买 nike

我有一个 firebase 模式,其中包含带有一些值的表“Catalog”

目录表

enter image description here

我使用 avgDrinkPrice 作为订单执行了 orderByChild(),但输出错误。

输出

enter image description here

代码

myRef.orderByChild("avgDrinkPrice").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
catalogList.clear();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
Catalog catalog = postSnapshot.getValue(Catalog.class);
catalogList.add(catalog);
Log.d(TAG,"Drink price " + catalog.getAvgDrinkPrice());
catalogSize++;
}
//LOOP ENDS.

我尝试使用其他数值(例如纬度字段、经度和等级)进行排序,它们的顺序是正确的。 只有按avgDrinkPrice排序才会错。我不明白为什么。请帮忙。

最佳答案

根据您的屏幕截图,avgDrinkPrice 属性是一个字符串,因此结果 排序正确。只是它们是按字典顺序排序的,而不是按数字排序的。

有更多关于 Firebase 在按 child 排序时如何排序数据的信息,here :

When using orderByChild(), data that contains the specified child key will be ordered as follows:

  1. Children with a null value for the specified child key come first.
  2. Children with a value of false for the specified child key come next. If multiple children have a value of false, they are sorted lexicographically by key.
  3. Children with a value of true for the specified child key come next. If multiple children have a value of true, they are sorted lexicographically by key.
  4. Children with a numeric value come next, sorted in ascending order. If multiple children have the same numerical value for the specified > child node, they are sorted by key.
  5. Strings come after numbers, and are sorted lexicographically in ascending order. If multiple children have the same value for the specified child node, they are ordered lexicographically by key.
  6. Objects come last, and sorted lexicographically by key in ascending order.

要让子项按数字排序,您必须将 avgDrinkPrice 存储为数字。 latilongtirating 也最好存储为数字。

使用 latilongtirating 属性的排序可能看起来是数字,但那是因为字符串值是ordered 可能具有相同的位数 - avgDrinkPrice 不是这种情况。

关于android - firebase orderByChild 只为一个 child 返回奇怪的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com