简体   繁体   English

如何使用Firebase排序数据?

[英]How can I sort data with Firebase?

I am creating a little React application about Pokemons. 我正在创建一个关于口袋妖怪的React应用程序。 I have in DB informations about all of them (about 900+). 我在数据库中有关于所有这些的信息(大约900多个)。

They all contain an id field, which is an integer from 1 to 900+. 它们都包含一个id字段,该字段是1到900+之间的整数。

But the problem is when I do a request like this : 但是问题是当我发出这样的请求时:

firebase.database().ref(`mydb`).orderByChild('id').startAt(1).limitToFirst(limit).once('value')

The results are not correct: I have an id array like this: [9,1, 10, 6, 4] 结果不正确:我有一个id数组,像这样: [9,1, 10, 6, 4]

Am I doing something wrong ? 难道我做错了什么 ?

Edit: I add the result I got when I perform the request I wrote above, I added a custom_id field containing ids as strings, but I stille have an unordered result: 编辑:我添加我执行上面编写的请求时得到的结果,添加了一个包含id作为字符串的custom_id字段,但是我仍然得到无序的结果:

[
  {
    "base_experience": 239,
    "custom_id": "009",
    "height": 16,
    "id": 9,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/9/encounters",
    "name": "blastoise",
    "order": 12,
    "weight": 855
  },
  {
    "base_experience": 64,
    "custom_id": "001",
    "height": 7,
    "id": 1,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/1/encounters",
    "name": "bulbasaur",
    "order": 1,
    "weight": 69
  },
  {
    "base_experience": 39,
    "custom_id": "010",
    "height": 3,
    "id": 10,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/10/encounters",
    "name": "caterpie",
    "order": 14,
    "weight": 29
  },
  {
    "base_experience": 240,
    "custom_id": "006",
    "height": 17,
    "id": 6,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/6/encounters",
    "name": "charizard",
    "order": 7,
    "weight": 905
  },
  {
    "base_experience": 62,
    "custom_id": "004",
    "height": 6,
    "id": 4,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/4/encounters",
    "name": "charmander",
    "order": 5,
    "weight": 85
  },
  {
    "base_experience": 142,
    "custom_id": "005",
    "height": 11,
    "id": 5,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/5/encounters",
    "name": "charmeleon",
    "order": 6,
    "weight": 190
  },
  {
    "base_experience": 142,
    "custom_id": "002",
    "height": 10,
    "id": 2,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/2/encounters",
    "name": "ivysaur",
    "order": 2,
    "weight": 130
  },
  {
    "base_experience": 63,
    "custom_id": "007",
    "height": 5,
    "id": 7,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/7/encounters",
    "name": "squirtle",
    "order": 10,
    "weight": 90
  },
  {
    "base_experience": 236,
    "custom_id": "003",
    "height": 20,
    "id": 3,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/3/encounters",
    "name": "venusaur",
    "order": 3,
    "weight": 1000
  },
  {
    "base_experience": 142,
    "custom_id": "008",
    "height": 10,
    "id": 8,
    "is_default": true,
    "location_area_encounters": "https://pokeapi.co/api/v2/pokemon/8/encounters",
    "name": "wartortle",
    "order": 11,
    "weight": 225
  }
]

You've not attached your database image, but with the order one thing is sure that these keys in your database are strings. 您尚未附加数据库映像,但是按照顺序,一件事是确保数据库中的这些键是字符串。 And when you order string data, it is ordered lexicographically. 当您订购字符串数据时,将按字典顺序对其进行排序。

So for numbers, this is the normal order: 因此,对于数字,这是正常顺序:

1 1个
9 9
10 10

But for strings, this is the normal order: 但是对于字符串,这是正常的顺序:

"9" “ 9”
"1" “ 1”
"10" “ 10”

I don't think there is any operator in Firebase (nor in most other databases) to change this behaviour. 我认为Firebase(大多数其他数据库中也没有)没有任何操作员可以更改此行为。

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" “ 001”
"009" “ 009”
"010" “ 010”

EDIT: Now after Json, these values are stored in id field so the above thing does not apply to this. 编辑:现在,在Json之后,这些值存储在id字段中,因此上述内容不适用于此。

You may however use a code like this, to do what you're trying: 但是,您可以使用如下代码执行您要尝试的操作:

mDatabase
  .child("main_child")
  .orderByChild("id")
  .addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot snapshot) {
      for (DataSnapshot child: snapshot.getChildren()) {
        System.out.println(child.getKey());
      }
    }
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM