Firebase query ordering not working properly

后端 未结 2 543
[愿得一人]
[愿得一人] 2020-12-07 04:23

Basically I have played with Firebase for the past week, and I recently stumbled upon the \'queryOrderedByChild()\' that as far as I know - allows you to sort data in fireba

相关标签:
2条回答
  • 2020-12-07 04:53

    If you using order by child you going to order your id you no going to touch it's value.

    Then maybe you have to try something like

    • (FIRDatabaseQuery *) queryOrderedByValue
      queryOrderedByValue: is used to generate a reference to a view of the data that's been sorted by child value.
    0 讨论(0)
  • 2020-12-07 04:54

    You say that you're ordering by a number, but the value of your id property is stored as a string.

    Since you're storing them as a string, they will be returned in lexicographical order.

    If you want them to be in numerical order, you should store them as numbers

    "-KHVUwXdVPHmrO_O5kil" : {
      "id" : 0,
      "name" : "Jeff"
    },
    

    Alternatively, you could store the ids as zero-padded strings:

    {
      "names" : {
        "-KHVUwXdVPHmrO_O5kil" : {
          "id" : "0000",
          "name" : "Jeff"
        },
        "-KHVV7lCeac0cZNMi9fq" : {
          "id" : "0003",
          "name" : "Stig"
        },
        "-KHVVCjXgl0XxasVOHF1" : {
          "id" : "0013",
          "name" : "Ali"
        },
        "-KHVVJtyUO-yJZiompJO" : {
          "id" : "0007",
          "name" : "Hannah"
        },
        "-KHVVR8tMSO1Oh7R8tR1" : {
          "id" : "0002",
          "name" : "Amanda"
        }
      }
    }
    

    Since the strings are all the same length, they will be sorted in the correct order. But you'll have to decide on the length of the string/maximum id value in the latter solution, so it seems worse.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题