简体   繁体   中英

How can I sort data with Firebase?

I am creating a little React application about Pokemons. I have in DB informations about all of them (about 900+).

They all contain an id field, which is an integer from 1 to 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]

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:

[
  {
    "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
9
10

But for strings, this is the normal order:

"9"
"1"
"10"

I don't think there is any operator in Firebase (nor in most other databases) to change this behaviour.

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"
"009"
"010"

EDIT: Now after Json, these values are stored in id field so the above thing does not apply to this.

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());
      }
    }
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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