Firebase Orderbychild Returns Weird Order For Only One Child
Solution 1:
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
.
Post a Comment for "Firebase Orderbychild Returns Weird Order For Only One Child"