Sorting Tables Ascending and Descending by column contents using Merge Sort

I have not seen any tools for sorting tables (lists of lists) ascending and descending by columns other than column 1, so here is a blocks library and a way to specify the sort order.

This only sorts by a single column at a time, and uses no extensions or Web View components.
The sort is via value procedure, so the output of one sort can be fed directly into another sort or one of the many table view techniques available on this board.

This also works on one dimensional lists.

Ascending Sort order specifications can be:

  • empty text
  • asc
  • ascending
  • a positive column number (for sorting tables)

Descending sort order specifications can be:

  • a negative column number (-2 = sort by column 2, descending)
  • anything else that doesn't match anything above (desc, descending, -, etc.)

Sample calls, in increasing complexity order:

Capture2
(Note how upper and lower case of the same letter are brought together in the sort, and numbers sort separately and numerically)

Capture3
Example of sorting by column 2. Notice how unfortunately, I have not yet figured out how to maintain the order of equal valued items. Good thing this is open source, huh?

Capture4
Sorting by column 2 in descending order.

The source:
merge_sort_customizable.aia (7.8 KB)

Draggable procedures:



Please note that I did not include any display component. This is strictly a back end blocks library.

6 Likes

(added to FAQ)

1 Like

there is my webviewer examle here from the old forum https://groups.google.com/g/mitappinventortest/c/pVA3VUMN5W8/m/pZLttKYl3-8J
which needs a small update, because now we have the WebViewStringChanged event...

Taifun

2 Likes

And it's easier to change list or list of list to json string:
Just tick the property of 'show list as json' in screen1 designer view.

1,
Snipaste_2022-04-30_10-29-31

2.first two block group are useful to sort list:
this can sort number and text

Snipaste_2022-04-30_10-31-30

1 Like

Your code runs well to order by second column, how can I order by another column ?

You mean my code? Just change the columnindex

I quoted Taifun, I'll test your code

Can you check my blocks ? no output.

Thanks for the Javascript examples.

I won't ask how to sort in descending order, since the output could be run through the reverse list block to achieve that.

There is a case for customization of the string comparison core of the sort, for people who care about clustering the lower and upper case versions of the same letter. I deal with that by downcasing text values before comparing them, but leaving their values intact...
a
A
b
B

vs

a
b
A
B

P.S. For further customization of the Javascript sort call, there is a good reference page at Array.prototype.sort() - JavaScript | MDN

Unfortunately, AI2 has no facilities for passing a compare function as parameter in a procedure call, so JavaScript wins in the higher complexity arena.

2 Likes

That can be done by anonymous procedure which have been implemented by Colintree several years ago, but MIT did not merge this for some reason.

Hi Kevin, I did not understand your suggestions

In my suggestion,the "I" in "AppInventor" should be capitalized, and before "sort" there is a dot.

You may use this block, just drag and drop this photo to your blocks viewer.

and I notice that you are using "list from csv row" block,this maybe cause some mistake.
This block will take the number as a string,you will get this:
"12"<"15"<"6"

So better to use "Web.jsonTextDecode" block to get a list from string.

1 Like

Thank You, it runs well,

Is it correct ?

I didn't know you could just use "AppInventor" instead of "window.AppInventor" with the webviewstring? Does this work in all settings, html files etc. ?

Or write the json string as [[1, aaa, 22], [2, bbb, 34]], This can be changed to a list of list.

@TIMAI2 'window' can be omitted. This can be used for all html only if loaded by Webviewer.

How descending sort ?

By using reverse list block

Change > to <.

OK, thank you