firebase orderByChild只为一个孩子返回奇怪的顺序 [英] firebase orderByChild returns weird order for only one child
问题描述
我有一个包含Catalog表的Firebase模式,其中包含一些值
$ b
产品目录表
:
使用orderByChild()
时,包含指定子键的数据将按如下顺序排列:
- 具有指定子键的
null
值的子项优先。
- 值为<接下来是code> false 。如果多个孩子的值为
false
,则按键按照字典顺序排序。
- 对于指定的孩子,值为true关键接下来。如果多个孩子的值
true
,则它们按键按字典顺序排序。
- 接下来是具有数字值的孩子,按升序。如果多个孩子对于指定的>子节点具有相同的数字值,则按键排序。
- 字符串在数字后面,按字典顺序按升序排列。如果多个子节点对于指定的子节点具有相同的值,那么它们将按照键的顺序排列。
- 对象最后一个按键按升序排列。
$ 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:
- Children with a
null
value for the specified child key come first.- Children with a value of
false
for the specified child key come next. If multiple children have a value offalse
, they are sorted lexicographically by key.- 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.- 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.
- 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.
- 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屋!