Firebase数据库按值顺序工作错误 [英] firebase database order by value working wrong

查看:69
本文介绍了Firebase数据库按值顺序工作错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个商店数据库,每个商店的名称前都有一个数字.它们是按随机顺序添加的,但是我希望它们以从1到213的顺序显示.我的问题是,当我使用orderbyvalue()时,它们显示为类似的样子.

So I have a database of stores and each store has a number in front of its name. They are added in a random order, but I want them displayed in order from 1 to 213. My problem is when I use orderbyvalue() it displays them like.

  • 1家商店
  • 10家商店
  • 11家商店
  • 12家商店
  • 13家商店
  • 14家商店
  • 15家商店
  • 16家商店
  • 17家商店
  • 18家商店
  • 19家商店
  • 2个商店
  • 20家商店
  • 21家商店
  • 22家商店
  • 1 Store
  • 10 Store
  • 11 Store
  • 12 Store
  • 13 Store
  • 14 Store
  • 15 Store
  • 16 Store
  • 17 Store
  • 18 Store
  • 19 Store
  • 2 Store
  • 20 Store
  • 21 Store
  • 22 Store

以此类推.我需要它们显示1,2,3,4,5等,而不是1,10,11,12,13等.

And so on. I need them it show up 1,2,3,4,5, etc instead of 1,10,11,12,13 etc.

我应该用什么来解决这个问题?

What should I use to fix this?

推荐答案

正如John所评论的,您所期望的顺序是:由于您要存储字符串,所以节点按照字典顺序进行排序.

As John commented, the order you get is expected: since you're storing strings, the nodes are ordered lexicographically.

如果要按数字顺序排序,则必须将值存储为数字,或者(更可能的话)将值存储为按字典顺序按数字顺序排序的格式.例如

If you want to order numerically, you either have to store the values as number or (in your case more likely) store the value in a format that orders the same lexicographically as numerically. E.g.

  • 01商店
  • 02商店
  • 10家商店
  • 11家商店
  • 19家商店
  • 20家商店
  • 21个商店
  • 22家商店

  • 001商店
  • 002商店
  • 010商店
  • 011商店
  • 019商店
  • 020商店
  • 021商店
  • 022商店

在这样的情况下,这种字符串的填充/前缀是很正常的.主要缺点之一是在启动项目时必须确定要为数字使用多少个字符.

This padding/prefixing of strings is quite normal in situations like this. One of the main disadvantages is that you'll have to determine how many characters to use for the numbers when you start your project.

这篇关于Firebase数据库按值顺序工作错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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