Firestore-降序排列 [英] Firestore - Orderby Descending

查看:52
本文介绍了Firestore-降序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Firestore数据库中,我的字段索引为no,数据类型为字符串,但他的数据为1、2、3等数字.

In my Firestore database I have field index no, datatype is string but he data is number like 1, 2, 3 and so on.

collectionReference
     .orderBy("indexNo", Query.Direction.DESCENDING)

输出到9为止都是正常的,即9,即10、11、12时,数据显示为1、10、11、12、2、3等.

the output is ok till 9, after 9 i.e., for 10, 11, 12, the data is displayed 1, 10, 11, 12, 2, 3 and so on.

请引导我解决此问题,因为这些数字将决定回收站视图中卡片视图项目的排列.

Pleas guide me to resolve it, as these numbers will decide the arrangement of cardview items in recycler view.

谢谢...

推荐答案

如果您的 indexNo 属性的数据类型为String类型,并且您要使用 Query排序结果.Direction.DESCENDING ,该结果将不会被删除,因为当您订购字符串时,结果将按字典顺序的顺序进行排序.有两种方法可以解决此问题.第一个方法是将属性的数据类型从String更改为 number ,这是我个人推荐的方法.

If the data type of your indexNo property is of type String and if you want to order the result using Query.Direction.DESCENDING, the result will be not the expeted one because when you order strings, the results are ordered lexicographically. There are two ways in which you can solve this issue. The first one would be to change the data type of your property from String to number, which I personally recommend it.

这是数字的正常顺序:

  • 1308
  • 1309
  • 1310
  • 1311

这是字符串的正常顺序:

And this is the normal order for strings:

  • "1308"
  • "1309"
  • "131"
  • "1310"

第二种方法是修改数据以获得所需的行为.对于数字,您可以通过用零填充数字来实现,如下面的示例所示:

The second approach would be to modify the data to get the behavior you want. For numbers, you can accomplish that by padding them with zeroes, like in the example below:

  • "0131"//之前加零
  • "0132"//之前加零
  • ......
  • "1308"
  • "1309"
  • "1310"
  • "1311"

这篇关于Firestore-降序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆