Kodular Sorting with TinyDB
Sorting a List in Kodular, at first glance, should be an easy thing to do. After all, Kodular is a feature-packed block-type editor that can easily create Android apps with no coding skills required. But this task is overly complicated. We've found an easy solution that requires just a few blocks that do not use and extensions or complicated functions. TinyDB is all that is needed.
Before getting to our solution, let's go over the different technologies and other solutions available.
There are many ways to store data in Kodular. One of the easiest ways to store data locally is to use TinyDB. The data stored in a TinyDB is persistent and will be available each time the app is running. An example might be a game that saves the high score and retrieves it each time the game is played.
There are many ways to approach sorting and the subject can get quite complex. One method is called Bubble Sort is very easy to understand. The basic concept is to start at the beginning of the list, compare each pair of adjacent items, and swap if they are not in order. You stop comparing when there are no more items left to compare. Below is an example of the sort method.
A popular solution is to use the SortAndGetList method as described in the great tutorial from Imagnity. It serves the purpose and offers flexibility. But also note, as shown below, it can get complicated to follow and requires many blocks.
While working within Kodular, I noticed an interesting behavior when working with TinyDB. Add it turns out, we can use an easy process to get what we want with a few lines of code. We can store and retrieve Tags to sort our lists.
While the solution may not offer different sort orders and other features, it does offer a clean and simple way of sorting lists while solving a challenging and difficult problem.
In this example, we will create a very simple application in Kodular to demonstrate our sort. Add you can see below, we've created an interface showing our original unsorted list and then show a sorted list after dumping it to TinyDb.
Our solution is this: by looping through our list we can add each item as a tag. When we retrieve out tags, Tinydb and kodular return the list alphabetically. Awesome, right!
Below are our blocks for the demonstration. We have a simple function that receives the list you give the function. The function will first clear all data from a dedicated TinyDB instance and then loops through each item, adding the item as a Tag.
Then, after our function call, we pull all tags from Tinydb and they are returned sorted.
When we run out test application, you can see our unsorted list now sorted :
One important note when using this method. There can be no duplicate tags in Tinydb. This can be good in the respect that we can also use this method to de-duplicate our list. But this method will not work if your values are not unique.
Knowing the limits of this method is important. But if you are looking to simply sort a list and do not have duplicate values, you can't beat the speed and reliability of this solution. If you need more, be sure to check out the procedure offered at https://imagnity.com/tutorials/app-inventor/list-sorting-on-app-inventor/
Please let us know if you've found this useful by leaving a comment and happy coding!