如何使用Firebase排序数据?

时间:2018-10-19 07:35:15

标签: javascript firebase firebase-realtime-database

我正在创建一个关于口袋妖怪的小React应用程序。我在数据库中有关于所有这些的信息(大约900多个)。

它们都包含一个id字段,它是1到900+之间的整数。

但是问题是当我发出这样的请求时:

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

结果不正确:我有一个id数组,像这样:[9,1, 10, 6, 4]

我做错什么了吗?

编辑: 我添加了我执行上面编写的请求时得到的结果,添加了一个包含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
  }
]

1 个答案:

答案 0 :(得分:0)

您尚未附加数据库映像,但是按照顺序,一件事可以确保数据库中的这些键是字符串。 而且,当您订购字符串数据时,将按字典顺序对其进行排序。

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

1
9
10

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

“ 9”
“ 1”
“ 10”

我认为Firebase(大多数其他数据库中也没有)没有任何操作员可以更改此行为。

相反,您将必须修改数据以获得所需的行为。因此:按字典顺序排序时,请按照您需要的顺序存储值。

对于数字,您可以通过用零填充数字来实现:

“ 001”
“ 009”
“ 010”

编辑: 现在,在Json之后,这些值存储在id字段中,因此以上内容不适用于此。

但是,您可以使用如下代码来执行您要尝试的操作:

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