且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android 中的 Firebase 数据描述排序

更新时间:2022-04-17 22:01:22

Firebase 可以按给定的属性按升序排列项目,然后返回前 N 个项目 (limitToFirst()) 或最后 N 个项目 (limitToLast()).没有办法表明您想要按降序排列的项目.

Firebase can order the items in ascending order by a given property and then returns either the first N items (limitToFirst()) or the last N items (limitToLast()). There is no way to indicate that you want the items in descending order.

有两个选项可以获得您想要的行为:

There are two options to get the behavior you want:

  1. 使用 Firebase 查询获取正确的数据,然后在客户端对其重新排序

  1. Use a Firebase query to get the correct data, then re-order it client-side

向数据中添加具有降序值的字段

Add a field that has a descending value to the data

对于后一种方法,通常使用反向时间戳.

For the latter approach, it is common to have a inverted timestamp.

-1 * new Date().getTime();