List orderer script request

Can anyone help me with a script that would order all the numbers in a list from smallest to largest that would also work in Scratch?

Well there are several algorithms to sort items in a list, like bubble sort or merge sort or quick sort.

Can you show me any in block form?

Here's a bubble sort block that I made:

Bubble sort is a simple sorting algorithm. It goes through each pair of items on a list, and if the first item is bigger than the second, it swaps them. It repeats this until it detects that it didn't do any swaps meaning the list is sorted.

it'd be better if your sort block was a reporter

Here is link to scratch ones -they should work in Snap! without too much effort to convert them

Any reason you use an arithmetic function (-) rather than a predicate (< or >) as the test function input? Your way, if you want to sort from high to low, you have to use

as the function input, which isn't very self-documenting.

I was trying to make the block inputs the same as the JavaScript sort function. To sort a numerical array of numbers in JavaScript you could use

arr.sort(function(a, b) {
    return a - b

so I wanted the block to behave similarly to that. But you are right, it would make more sense if I used a predicate instead of a reporter.

Ah, I see. They want to detect the case in which the two records are equal.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.