firebase orderByChild只为一个孩子返回奇怪的顺序 [英] firebase orderByChild returns weird order for only one child

查看:177
本文介绍了firebase orderByChild只为一个孩子返回奇怪的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含Catalog表的Firebase模式,其中包含一些值
$ b

产品目录表




使用 orderByChild()时,包含指定子键的数据将按如下顺序排列:


  1. 具有指定子键的 null 值的子项优先。
  2. 值为<接下来是code> false 。如果多个孩子的值为 false ,则按键按照字典顺序排序。
  3. 对于指定的孩子,值为true关键接下来。如果多个孩子的值 true ,则它们按键按字典顺序排序。

  4. 接下来是具有数字值的孩子,按升序。如果多个孩子对于指定的>子节点具有相同的数字值,则按键排序。

  5. 字符串在数字后面,按字典顺序按升序排列。如果多个子节点对于指定的子节点具有相同的值,那么它们将按照键的顺序排列。

  6. 对象最后一个按键按升序排列。

  7. $ b

要让孩子以数字顺序排列,您必须存储 avgDrinkPrice 作为一个数字。和 lati longti rating 数字也是如此。



使用 lati longti rating 属性可能看起来是数字的,但这是因为被排序的字符串值可能具有相同的位数 - 这与 avgDrinkPrice


I have a firebase schema containing the table 'Catalog' with a few values

Catalog Table

I did an orderByChild() using avgDrinkPrice as the order and the output was wrong.

Output

Code

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.

I have tried sorting using other numerical values such as the latitude field, longitude and rating and their orders are correct. Only when sorting by avgDrinkPrice would the order be wrong. I don't understand why. Please help.

解决方案

According to your screenshot, the avgDrinkPrice property is a string, so the results are sorted correctly. It's just that they are sorted lexographically - not numerically.

There is more information on how Firebase sorts data, when ordering by 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.

To have the children ordered numerically, you would have to store avgDrinkPrice as a number. And lati, longti and rating are probably better stored as numbers, too.

The orderings that use your lati, longti and rating properties might appear to be numerical, but that would be because the string values being ordered probably have the same number of digits - which is not the case with avgDrinkPrice.

这篇关于firebase orderByChild只为一个孩子返回奇怪的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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