How do I sort Firebase Keys in ascending order?
I have a list of products in my Firebase database. My SKU looks like this c08p10
. But the problem is that after the tenth product, a hundred products are displayed.
Please refer to the screenshot for the mixed order.
I understand that the products are ordered alphabetically. But how can I display products in the following order
c08p10
c08p11
c08p12
Note
I can fill in the data in the List view or Recyclers view. But the products are located in the order of the Firebase DB. If I try to change the order. It gives below structure.
c08p118
c08p117
c08p116
source to share
Firebase keys are strings. And when you order string data, it is lexicographically ordered.
So, for numbers, this is the normal order:
- 1
- nine
- ten
- eleven
But for strings, this is the normal order:
- "1"
- "eleven"
- "nineteen"
- "nine"
There is no operator in Firebase (or most other databases) to change this behavior. Instead, you will need to modify the data to get the behavior you want. So: keep the values ββthat are in the order you want them when sorting lexicographically. For numbers, you can do this by padding them with zeros:
- "001"
- "009"
- "011"
- "019"
Or in your case c09p010
, etc.
source to share