[SOLVED 2/8/24 8:12 AM EDT] Sorting lists from firebase

I’ve seen posts relating to this but I don’t know whether it answers my question or not. So apologies if this is a repeated post.

I have data on a firebase realtime database. A leaderboard for who has won the most challenges in an app I am creating. I want to be able to pull this data down from the server and arrange the list so that the one with the highest amount of wins is at the top (such as in a traditional leaderboard). At present the data on the server is held in JSON format which contains all manner of data relating to their profile (I’d seen through my exploring that JSON appears to be a quick way for the app to read data?).

The problem is that I can’t organize the data to display in the correct sequence.

An example to help;
User aAa1 has won 46 battles.
User bBb2 has won 67.
User cCc3 has won 4.
In firebase realtime database I have a folder path “cars/carDetails/aAa1” with a JSON script {“user”:“aAa1”,“battlesWon”:46}
I have found out how to make the app differentiate what value I am looking for. I feel confident I could get the list to numerically order it.

The problem is that I want the list to display “User aAa1 has won 46 battles!” or something along them lines. But how do I tell the list to match the correct username with the amount of battles won?

I tried creating a seperate folder without the use of JSON and coded it straight in. For example the folder path “leagues/rr/standings” had items titled “useraAa1BATTLESWON46”. The problem is thunkable couldn’t numerically arrange this list presumably because of the use of letters.

Any help would be appreciated. I have tried following DrTeds JSON tutorial which I think was helping me understand JSON but it doesn’t appear to be finished.

Here’s the algorithm I would use:

  1. For each entry in Firebase, create an object as a list item in Thunkable. The object should have two properties: “username” and “battlesWon.” There is no need to sort this list. As you loop through Firebase entries, also create a second list with just the battlesWon.
  2. Sort the second list numerically.
  3. To display a leaderboard, loop through the second list and for each item’s value, find that value in the list of objects’ “username” property. Use the index (list item #) of the found value to get the “battlesWon” property from that same list item’s object.
  4. Join the username value with the battlesWon value and display it in a label.

If you want a simpler method, just name your Firebase entries with this format: “46 battles won by User aAa1”. Then transfer those to a list in Thunkable, sort the list numerically, and display the list in a Simple List component. With the numbers at the front, they should sort very easily.

2 Likes

That actually makes perfect sense thankyou! You’re very good you’ve helped me with a few things on here! I’ll give it a go and report back but I can see the logic in what you’ve put.

Hi Tatiang,

First of all thankyou for your help. I have tried both ways and I am still having trouble with it correctly displaying a leaderboard. I am confident it is my lack of understanding but wondered if you could simplify some explanations. I was struggling with point 3 above. Would you know any way of elaborating on that?
I also tried the simplified version you suggested which does get the scores to display in the leaderboard but always with the higher score at the top and never at the bottom. To clarify, I do actually want it at the top but I tried changing it to see if I had as much control as I do and no matter what I do it always seems to be at the top. Here are my blocks:

I really would like to be able to use JSON better so would prefer to figure out how to make the first option work. But either resolution will be appreciated the same way.

I think the ‘sort numerically’ block is now only working with items that contain only numbers.

Used to work with numbers & alphabetic characters as long as the numbers were at the front.

Just discovered this as working on updating an app that I havent opened for a few months and this is one of the things that works in the current published app but not in the version currently opened on the platform.

Ah I see. That would certainly explain a couple things. Thank you. So I’m that case really I need to find out how to use JSON better or find a way to rearrange the score only and find out how to identify which uid should have the score when it is in its new position. I know it has something to do with JSON, I’m just not yet skilled enough to know what exactly.

Right! Haven’t had opportunity to try it yet. Will do soon but found a guy called @darren on YouTube who has some videos that I feel confident will be able to help. Will report back