Sorting in descending order in Firebase database [duplicate]

不想你离开。 提交于 2020-01-27 23:11:46

问题


There is this list of Books in the Firebase Realtime Database:

[
  {
    title: "Don't Make Me Think",
    upvotes: 110
  },
  {
    title: "The Mythical Man Month",
    upvotes: 111
  },
  {
    title: "Code Complete 2",
    upvotes: 112
  }
]

By default, the query below returns this list in ASC order:

firebase.database().ref('books').orderByChild('upvotes')

How can I query and order them by upvotes DESC instead?


回答1:


Unfortunately firebase doesn't allow returning by descending order. I have generally just reversed the order of the results returned client side.

If your data query is too large to sort client side you can do

firebase.database().ref('books').orderByChild('ups').limitToLast(2)

and then invert the results from there. Documentation can be seen here



来源:https://stackoverflow.com/questions/45357920/sorting-in-descending-order-in-firebase-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!