如何按升序对Firebase密钥进行排序?

编程入门 行业动态 更新时间:2024-10-23 07:33:59
本文介绍了如何按升序对Firebase密钥进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的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密钥进行排序?

本文发布于:2023-11-29 00:51:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:升序   密钥   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!