Bubble sort list without an extension (numbers/strings)

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)
Bubble sort

Let’s get started. Blocks you’ll need:

Three global variables
Global block

Procedure block with three input

Explanation:

  1. We need a procedure block with three input and return result
    1
  • 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.
  1. We used a do block which acts as a container for result slot.
    2

  2. 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.
    3

  3. 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.
    4

  4. 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.
    5

  5. Re-initialize swap needed? to false for later use.
    6

  6. Store dataholder1 to the first item, using the index we have just obtained.
    Same thing, store dataholder2 to the second item using the index2.
    7

  7. We have to check whether the demanding order is ascending
    8

  8. If so, check if the itemType is number.
    9

  9. 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. Set swap needed to true so that we can swap later.
    10
    If itemType is not Number,

  10. 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 to true so that we can swap later.


    If we need to sort the items in descending order,

  11. Same as step 9, check first if the comparison for Descending order should be in number mode.
    12

  12. 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.
    13
    If itemType is not number,

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

  14. Check whether swap needed is true. If true, we replace the item at position index1 with the item at position index2 (dataHolder2) and replace the item at position index2 with the item at position index1 (dataHolder1)
    15

  15. Finally, set result to listToBeSorted in order to return the sorted list.
    16

We have finished the procedure block. Let me show you how we can call the procedure Bubble_Sorting

  • For sorting number in ascending order
    Call procedure 1
  • For sorting text in ascending order
    Call procedure 2
  • For sorting number in descending order
    Call procedure 3
  • For sorting text in descending order
    Call procedure 4
    *On the above blocks, variable list is a list.

Note:

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

  2. 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.
ListContainNumbersOnly_procedure

Check_if _list_contain_number_only

Happy koding :kodular: !

17 Likes

Do you think this is a guide

1 Like

image
I thought it means to share something
So… which category shall I set?

1 Like

Hi @WatermelonIce,

A guide should be more then a link to a site. It is something you make yourself, with blocks, screenshots, explanations etc.

I unlist this if you want to improve on it.

2 Likes

Write your own words and blocks

Oic… I will improve it.

I’m sorry that I didn’t know that.

2 Likes

I have improved my guide. I use the link as the reference and reproduce by myself.

kodular
i am getting this error… please fix this. almost a week passed. still couldnt figure it out. i used your code.

well, my guess is, somewhere you made an error in copying the blocks… :grimacing:
it might be easier for you to use the List Utils Extension by Appybuilder
alternatively what about How to sort a list using the webviewer(!)

Taifun

1 Like

Yes, my code has nothing wrong. This guide is only for those who doesn’t want to use extension. You can do what taifun told to

Nice Guide

One important thing. I do get the desired results but this error message pops up everytime.

Can you atleast say on what cases this error appears?

Actualy i want to create a leaderboard which will show username and points. So i cant use sort extension as it will only sort the points and not the corresponding names. Help please.

the show your blocks, let me check

1 Like


Taifun

asd swd