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

**URL:** <https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780>\
**Category:** Questions about Thunkable X\
**Created:** [February 6, 2021, 12:40pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780 "2021-02-06T12:40:08Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![funhall](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/funhall/32/92145_2.png) [@funhall](https://community.thunkable.com/u/funhall)\
**Post date:** [February 6, 2021, 2:22pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/22 "2021-02-06T14:22:24Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/3/9/39a2f4bf270f4298840cb989bafba6cd5e5913d4.png)  
But the app still crashing every time i push the button

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [February 6, 2021, 2:27pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/23 "2021-02-06T14:27:12Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/1/f/1f2bc6a4e87e252e2d9188184cd55b66a6d5492f.png)

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

---

<div class="post-metadata">

**Author:** ![funhall](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/funhall/32/92145_2.png) [@funhall](https://community.thunkable.com/u/funhall)\
**Post date:** [February 6, 2021, 2:27pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/24 "2021-02-06T14:27:37Z")

</div>

i do

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [February 6, 2021, 2:30pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/25 "2021-02-06T14:30:04Z")

</div>

Now that you know it works the other issue is that you are assigning a `list` to a text field so instead of  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/b/9/b927c1d35df3e5e5f0ed5daf018ebb409ec3e1bb.png)

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](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/7/1/71037a1ce8a92eec0ef593863a85b99945da7614.png)
- The `Label6` block should be outside the loop when the loop finishes.

---

<div class="post-metadata">

**Author:** ![funhall](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/funhall/32/92145_2.png) [@funhall](https://community.thunkable.com/u/funhall)\
**Post date:** [February 6, 2021, 2:32pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/26 "2021-02-06T14:32:54Z")

</div>

here I have a list with all the index numbers

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/0/2048b2d33d146cc1c6e1b6bb70f1f5d72aa5c6fc.png)

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [February 6, 2021, 2:47pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/27 "2021-02-06T14:47:14Z")

</div>

> [@muneer](#):
>
> **[Update]**
> 
> - It is a good practice to set the `app variable company` to` empty list` before starting the loop  
> ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/7/1/71037a1ce8a92eec0ef593863a85b99945da7614.png)
> - The `Label6` block should be outside the loop when the loop finishes.

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

---

<div class="post-metadata">

**Author:** ![funhall](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/funhall/32/92145_2.png) [@funhall](https://community.thunkable.com/u/funhall)\
**Post date:** [February 6, 2021, 2:49pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/28 "2021-02-06T14:49:29Z")

</div>

yes I also have done that

---

<div class="post-metadata">

**Author:** ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)\
**Post date:** [February 6, 2021, 2:53pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/29 "2021-02-06T14:53:20Z")

</div>

@funhall ,

Here is a working solution.  
[https://x.thunkable.com/copy/8b709d01c11250c7ce034ce363e6ef7c](https://x.thunkable.com/copy/8b709d01c11250c7ce034ce363e6ef7c)

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/c/2/c277eebde55c3940c4cb9ec06a015db9063942fe.png)

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](https://stackify.com/oop-concept-for-beginners-what-is-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](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/7/7/77dd4d3a3820f97d8992d98fda33c7315d3a35c5.png)
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.

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [February 6, 2021, 3:08pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/30 "2021-02-06T15:08:06Z")

</div>

@drted  
You’re a undisputed master of JSON (among other things).

---

<div class="post-metadata">

**Author:** ![funhall](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/funhall/32/92145_2.png) [@funhall](https://community.thunkable.com/u/funhall)\
**Post date:** [February 6, 2021, 3:13pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/31 "2021-02-06T15:13:23Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![funhall](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/funhall/32/92145_2.png) [@funhall](https://community.thunkable.com/u/funhall)\
**Post date:** [February 8, 2021, 3:20pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/32 "2021-02-08T15:20:34Z")

</div>

Hi again

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

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/9/2929594615a5329e4a8c38507c89b2be950314a3.png)

---

<div class="post-metadata">

**Author:** ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)\
**Post date:** [February 8, 2021, 3:43pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/33 "2021-02-08T15:43:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aarav-work](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/aarav-work/32/95738_2.png) [@aarav-work](https://community.thunkable.com/u/aarav-work)\
**Post date:** [February 10, 2021, 10:17am UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/35 "2021-02-10T10:17:37Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)\
**Post date:** [November 8, 2024, 1:29pm UTC](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780/36 "2024-11-08T13:29:35Z")

</div>



[Previous page](https://community.thunkable.com/t/how-to-get-a-very-long-list-from-firebase-and-sort-it/1084780.md?page=1)
