且构网

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

如何根据值< String,Long>对值进行排序在数组列表中?

更新时间:2023-10-08 20:07:40

Firebase实时数据库可以返回按每个子节点的属性值排序的子节点.但是,由于您是通过汇总数据库中的值来确定existingSteps的,因此除非您也将汇总值存储在数据库中,否则无法以此顺序从数据库中获取节点.

The Firebase Realtime Database can return child nodes ordered by the value of a property of each child node. But since you determine existingSteps by aggregating values from the database, there is no way to get the node from the database in this order, unless you also store the aggregated value in the database.

以当前数据格式,在汇总所有用户的步骤后,您将需要重新排序阵列列表客户端中的项目.

In your current data format, you will need to re-order the items in your array list client-side after aggregating the steps for all users.

如果您要修改数据格式以包括汇总的existingSteps值,请记住,Firebase只能按升序顺序返回结果.这意味着要以相反的顺序显示它们,您要么必须反转数组列表客户端,要么在数据库中存储一个具有倒置值existingSteps的附加属性,以便对其进行排序就可以得到相反的结果.有关更多信息,请参见 firebase->日期顺序倒退

If you were to modify the data format to include the aggregated existingSteps value, keep in mind that Firebase can only return results in ascending order. That means that to show them in reverse order, you'll either have to reverse the array list client-side, or store an additional property in the database that has the inverted value of existingSteps so that ordering on that gives the reversed results. For more on this, see firebase -> date order reverse