繁体   English   中英

如何使用Firebase排序数据?

[英]How can I sort data with Firebase?

我正在创建一个关于口袋妖怪的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个
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());
      }
    }
    ...

暂无
暂无

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

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