Hi guys! Just share a way to arrange a list without an extension. I found this link from this post:
This is using the method of bubble sorting (Bubble sort - Wikipedia). Here is a gif from WIKI explaining how it works (sped up for better view)
Let’s get started. Blocks you’ll need:
Three global variables
Procedure block with three input
Explanation:
- We need a procedure block with three input and return result
- listToBeSorted: The list we want to sort
- order: Sorting method (Ascending/Descending)
- itemType: Check whether the items are numbers or text. For number, it is easily be done by using Math operation (= , ≠ , > , ≥ , < , ≤) to compare two numbers. However, it cannot compare text. Therefore, we have to use the operators from the text block. For text, it uses alphabetical representation of each character in a piece of text. In text type, “b” is higher than “B”, “2” is higher than “10”. Each character has a decimal/numerical representation in Text mode. You can look at the Ascii-Table for details.
-
We used a
do
block which acts as a container forresult
slot.
-
First, we check if the list contains at least two items. If not, then there’s not really enough elements to sort as sorting of one element will return that element.
-
We need to compare each adjacent pair. For us to do that, we need one loop to get the items starting from the beginning of the list to the item second to the last in the list to compare. We are not going to compare the last item because there’ll be no item after the last item obviously to compare with. Therefore, we need the
for each index
. In the blocks, we named the variable as “index1” and it is from the first item in the list to the(length of list list = listToBeSorted) - 1
, to obtain the item second to the last in the list.
-
As mentioned, we need two items to compare. We have the first index in step four, so we have to get the second index. Differ from step four, this starts from
index1 + 1
because we want to compare the next item pointed by “index1”. We compare each until the end of the list including the last item. That is why we shouldn’t include the last item in the first for loop.
-
Re-initialize
swap needed? to false
for later use.
-
Store dataholder1 to the first item, using the index we have just obtained.
Same thing, store dataholder2 to the second item using the index2.
-
We have to check whether the demanding order is ascending
-
If so, check if the itemType is number.
-
If itemType is Number, then we have to compare two data using Math operator. Use the
>
to compare them. If dataHolder1 (item1) is greater/higher than dataholder2 (item2), we need to swap them as we are sorting in ascending mode. Setswap needed
totrue
so that we can swap later.
IfitemType
is not Number, -
We do the same comparison as step 10 but this time using the comparison operator from Text block. If the item at dataholder1 (item1) is higher, we set
swap needed
totrue
so that we can swap later.
If we need to sort the items in descending order, -
Same as step 9, check first if the comparison for Descending order should be in number mode.
-
If itemType is Number, do the opposite of step 10. Use the
<
operator from Math block since this time we want to swap only if the dataHolder1 (item1) is less/lower than dataHolder2 (item2) since we are sorting the items in descending order. If item dataHolder1 is lower, we set the global variable status to true so that we can swap later.
If itemType is not number, -
We do the same comparison as step 13 but this time using the comparison operator from Text block. If dataHolder1 (item1) is lower, we set the global variable status to true so that we can swap later.
-
Check whether
swap needed
is true. If true, we replace the item at positionindex1
with the item at positionindex2
(dataHolder2) and replace the item at positionindex2
with the item at positionindex1
(dataHolder1)
-
Finally, set result to
listToBeSorted
in order to return the sorted list.
We have finished the procedure block. Let me show you how we can call the procedure Bubble_Sorting
- For sorting number in ascending order
- For sorting text in ascending order
- For sorting number in descending order
- For sorting text in descending order
*On the above blocks, variablelist
is a list.
Note:
-
You can simplify the Bubble_Sorting procedure depending on your needs. If you need a procedure for sorting numbers in ascending order, remove the unnecessary blocks. Also, you can remove order and itemType arguments if you don’t need those arguments.
-
You might want to check if items in the list are number when you sorting in number type, as errors will occur if you sort a text list in number type.
Here is an example of how you can verify if items are numbers.
Happy koding !