Sorting rows in a table to be viewed with Data List Viewer

Hi everyone

I hope someone can help me with a problem I have with sorting and displaying data!

The user inputs a number and the app performs various calculations and returns some numerical answers, which are each stored as variables. I am using a LocalDB and populating a column in the LocalDB with the variables by using the ‘Set cell’ function. For example, the user enters the number 20 and the following results may be put into the LocalDB (in the right hand numbers column):

Apple 5
Pear 20
Banana 3
Peach 42
Kiwi 15

There will always be the same 5 fruits in the left column, only the numbers in the right column may change.

What I am trying – and failing – to do is find a way to sort the above data in 3 ways (left column: alphabetical; right column: high to low, low to high). Which sort would depend on the user’s preference. Once sorted, I would then want the output in the data viewer to be according to the sort. I.e. it data viewer would show the data as follows:

[alphabetical]

Apple 5
Banana 3
Kiwi 15
Peach 42
Pear 20

[high to low]

Peach 42
Pear 20
Kiwi 15
Apple 5
Banana 3

[low to high]

Banana 3
Apple 5
Kiwi 15
Pear 20
Peach 42

Can anyone help? I have not been able to find the answer through searching. Thanks!

1 Like

Use two lists, one for the fruits and one for the numbers. Sort each one as needed using the various List blocks. Then, update the local data source by using the Join (text) block to get the fruit and number for each index within a loop.

1 Like

Thanks for this explanation. I’m still feeling my way around Thunkable and had a few follow-up questions.

Do you mean that I should set a list from each column (fruits, numbers)? Is that done by [set to <localDB / Get column>], and then sort those two lists?

I am also not sure what to do once the lists are sorted. How do I update the local data source and then use that to feed into the data list viewer? Sorry for the extra questions, but hope you can help!

1 Like

For all of the reasons you have cited, local data source may not be the way to go.

Consider using a JSON object list and this sorting function.

1 Like

Thanks! I want to use a LocalDB for offline functionality so I’ll try tatiang’s approach first to see if it works. Otherwise, I’ll definitely try this. Thanks again.

1 Like

You can save JSON as a STORED variable for offline use too :wink:

Happy Thunking!

1 Like

Use a list of objects. The object has two properties (FruitName and FruitCount).

This way you can order the list by any property and use can store the list elements in local storage or stored variable.

Thanks to everyone for your help. I will try these ideas out and report back.

Hi @muneer - are you able to help explain your answer a bit further? I have set up an object food with a list of objects. However, I have no idea how to sort the list, or to find the properties in the list. I have looked at the documentation for lists and objects and watched videos on lists and objects but there is no explanation on how to make lists work with objects. Would you be able to help with blocks? I am so confused!

The blocks below do not work because I can’t find a way to search inside the list.

@slothball Here’s a demo. The “sort list” button is not a full sort. It just compares the first two list items (objects) and swaps them if they aren’t in alphabetical order already. But it should give you an idea about how you might sort a list of objects.

Your blocks didn’t work because when you get the object properties of “app food”, you’re assigning a list of values to “app food1” and then when you try to get property “fruit” of object “app food”, it’s not an object… it’s a list. Also, since “app food” is a list and not an object (rather, it’s a list of objects), taking the object properties of it isn’t very useful as it only returns the list indices (0,1,2).

1 Like

Here is a remixable project with and object sorting function.

2 Likes

Thanks @tatiang - the demo is really helpful - thank you for doing that.

Thanks @drted!

Here is another way making use of the sort functions available in Thunkable

Food Sort Sample

2 Likes

@muneer That’s very smart to organize the list values by [sort key] [secondary value]!

1 Like

@muneer thank you - so elegant!

1 Like