控制台中对节点进行排序

控制台中对节点进行排序

本文介绍了如何根据密钥在 firebase 控制台中对节点进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于密钥为 1 到 1400 的用户的节点,但它没有按顺序排列.热在控制台中按顺序查看.我在另一个节点上也有类似的经历.但后来它被自动订购了

I have a node for users with key 1 to 1400 but it's not in order.Hot to view this in order in the console. I had a similar experience with another node. But it later got ordered automatically

推荐答案

很遗憾,您无法在 Firebase 数据库控制台中更改节点的顺序.默认情况下,所有节点都按键排序.要记住的一件事是 Firebase 键是 Strings.当字符串有序时,按字典顺序排序.

Unfortunately, you cannot change the order of the nodes in the Firebase Database Console. By default, all the nodes are ordered by key. One thing to remember is that Firebase keys are Strings. And when strings are order, are ordered lexicographically.

所以对于数字,这是正常的顺序:

So for numbers, this is the normal order:

  • 1308
  • 1309
  • 1310
  • 1311

但是对于字符串,这是正常的顺序:

But for strings, this is the normal order:

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

Firebase 中没有运算符,据我所知,在大多数其他数据库中也没有允许您更改此行为的运算符.相反,您必须修改数据才能获得所需的行为.因此,在按字典顺序排序时,按照您需要的顺序存储值.对于数字,您可以通过用零填充它们来实现:

There is no operator in Firebase and as far as i know nor in most other databases that allow you 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:

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

这篇关于如何根据密钥在 firebase 控制台中对节点进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 11:01