简体   繁体   中英

Best way to sort data from Firebase

I have a data set being returned from a Firebase database, inside the data set is team information that contains number of games played, points, fouls conceded, etc...

Team - Points

Chelsa 10

Manchester 6

Liverpool 18

Madrid 22

etc...

I need to order these top to bottom by the number of points, so using the above example, Madrid would be first, then Liverpool, then Chelsea and lastly Manchester.

My question is; does Firebase allow me to do this? I have tried using;

OrderByChild("points");

But this does not order by points (asc or desc)

Or should I order the Java arrayList using sort?

There is no descending order in Firebase, according to this from the docs:

orderByChild()

Children with a numeric value come next, sorted in ascending order. If multiple children have the same numerical value for the specified child node, they are sorted by key.

then you can do:

orderByChild("points").limitToFirst(20);

which will give you the first 20, or you can use limitToLast(20);

more info here:

https://firebase.google.com/docs/database/android/lists-of-data

Firebase database provide only ascending order sort. So you have to get this data and reverse the data.

Collection.reverse(datalist);

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