How to get a very long list from firebase and sort it

Thanks @muneer I am really figthing here. To get it to work.
As I understand you it should be like this


But the app still crashing every time i push the button

1 Like

Letā€™s do some debugging and start with the basics.
We want to be sure that the app variable name actually contains a list so add a list viewer to your screen and add a button and put this code

If you see a list of number 0, 1, 2, 3 then this step is OK.

i do

1 Like

Now that you know it works the other issue is that you are assigning a list to a text field so instead of
image

assign the app variable company to the list viewer and see what comes up.

[Update]

  • It is a good practice to set the app variable company to empty list before starting the loop
    image
  • The Label6 block should be outside the loop when the loop finishes.

here I have a list with all the index numbers

1 Like

@funhall
Check the update I made to my response and let me know.

yes I also have done that

@funhall ,

Here is a working solution.
https://x.thunkable.com/copy/8b709d01c11250c7ce034ce363e6ef7c

A few things to notice.

  1. Iā€™ve added label updates that indicate progress. These are essential when debugging JSON objects.
  2. The ā€œfor each itemā€ loop eliminate the need for the ā€œin listā€¦get itemā€ block, so it make the code a little easier to read. If you end up nesting loops, it really makes a difference in readability
  3. Iā€™ve created a silly ā€œDistance Calculationā€ function. It is NOT EVEN CLOSE to correct. But the advantage is that you can create this function and pretend it returns the correct distance. Assuming the distance is correct, you can build out the rest of the function and display. Then you can go back and put it a better distance calculation later and the rest of the code will continue to work without any further modifications (encapsulation at work). See my previous post in this thread for a link to the haversine function discussion.
  4. The latitude and longitude you have stored in firebase are strings, as indicated by the quotation marks. You need to convert these to number by adding zero when performing comparisons.
    image
  5. The APP variable ClosestCustomer contains the entire object after the loop has completed, so you can access all of the properties using that object without worrying about its position in the list.
2 Likes

@drted
Youā€™re a undisputed master of JSON (among other things).

@drted thank you so much. I just have to go through the block to find out how everything works. But it looks like it works.
Thanks again.

Hi again

I am trying out your setup. But what is your meaning with this variable

That variable saves the closest customer to the app user. Notice that the variable is also set when the if condition in the loop is true. It is also the result displayed in label1

I initialize it before the loop so that the first time through the loop there is a value for the comparison.

I also had the same problem, if possible I suggest you use airtable, please donā€™t listen to me until someone from thunkable replies ā€œagreedā€