Bubble sort list without an extension (numbers/strings)

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

You list length is less than 2 and therefore this error pop up. btw, your blocks looks different to mine. Let me check.

And can you show the example list?


This is the result i get. I get the result correct but that run time error pops out evrytime

Please help
It’s not working

Great post! Thanks for the tips on sorting. I came across this post looking for a way to sort a dictionary by the key. Your logic helped me put together a couple of procedures for sorting dictionaries. I’m sharing my blocks for anyone else that may be looking for more guidance on sorting and sorting dictionaries. I also made some changes to your Bubble Sort to make it more readable (at least for me :wink:).

In the bubble sort, I eliminated the global variables and the extra inputs without losing any functionality. The only input to the Bubble Sort is an unsorted list.

I started with a “foreach item in list” to see if any list item contains non-numeric data to determine if we are sorting numbers or text. This eliminated the need to pass in “itemType”.

I condensed a bunch of the if statements down to one if using" And"s and “Or” to determine if a swap of items is needed. Below is the expanded blocks to the above if statement. Basically, one side of the “or” is for numeric comparison and the other side for not numeric.

The sort was simplified more by eliminating the “order” option. The results will be ascending. If you need results in descending order, attached the results to a “reverse list” block.

blocks3

This next procedure is to sort a dictionary by the key.

It just take the keys and runs them though the Bubble Sort procedure and then rebuilds a dictionary with the sorted keys. In this procedure, I have an optional “descending” input, default will be ascending. For descending, just attach a “true” block.

blocks5

For ascending, either attached “false” to descending or leave it without anything attached.

4 Likes

Also, the “if length of list >= 2” is not needed. If the list has one item or is empty, the “for each index1” block will have a “to” value less than “from” value. This will just cause the for each to have no iterations and the returned result will just be the original list.

1 Like

Wow! That’s awesome :smiley: Thanks for the appreciation and implementation of the bubble sort logic.

I’ve always loved to learn new logic. Very clever indeed!

2 years ago I’m still a newbie to programmings. At that time I really didn’t think of optimization stuff :sweat_smile:

1 Like