如何按升序对Firebase密钥进行排序? [英] How to sort Firebase Keys in ascending order?
问题描述
我的Firebase数据库中有一个产品列表.我的SKU看起来像这样 c08p10
.但是问题在于,百乘积显示在第十乘积之后.
I have a list of products in my Firebase Database. My SKU looks like this c08p10
. But the problem is that Hundred product is shown after the tenth product.
请参阅屏幕截图以了解混乱的产品订单.
Please refer the screenshot for the jumbled product order.
我可以理解,产品按字母顺序排列.但是如何按以下顺序显示产品
I can understand that the products are arranging alphabetically. But how can i display the products in the below order
c08p10
c08p11
c08p12
注意
我可以在列表"视图或回收者"视图中填充数据.但是产品按Firebase DB顺序排列.如果我尝试颠倒顺序.它给出了以下结构.
I can populate the data in a List view or recycler View. But the products are arranged in the firebase DB order. If I try to reverse the order. It gives the below structure.
c08p118
c08p117
c08p116
推荐答案
Firebase密钥是字符串.当您订购字符串数据时,将按字典顺序对其进行排序.
Firebase keys are strings. And when you order string data, it is ordered lexicographically.
所以对于数字,这是正常的顺序:
So for numbers, this is the normal order:
- 1
- 9
- 10
- 11
但是对于字符串,这是正常的顺序:
But for strings, this is the normal order:
- "1"
- "11"
- "19"
- "9"
Firebase(大多数其他数据库中也没有)没有操作员可以更改此行为.相反,您将必须修改数据以获得所需的行为.因此:按字典顺序排序时,请按照您需要的顺序存储值.对于数字,您可以通过用零填充数字来实现:
There is no operator in Firebase (nor in most other databases) to change this behavior. Instead, you will have to modify the data to get the behavior you want. So: store values that are in the order you need them when sorted lexicographically. For numbers you can accomplish that by padding them with zeroes:
- "001"
- "009"
- "011"
- "019"
或者您的情况是 c09p010
等
这篇关于如何按升序对Firebase密钥进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!