簡體   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