How to sort a list of names (no, I didn't misspell numbers)

1,537 views
Skip to first unread message

Stephen Zipprich

unread,
Jun 22, 2014, 11:48:33 PM6/22/14
to mitappinv...@googlegroups.com
If you do a search on the forums here for how to sort a list, you'll probably find a few different solutions:

Perhaps you'll find a few others.  But the main approach with sorting is generally a sorting of a number list, rather than a list of names (or at least a list of non-numeric items).  One of the above links has a link to a text sort using AI Classic blocks, but as the author says on his site, it takes a long time.  With AI2, it's possible to sort a list of names and do it quickly.  How you ask?  With the text compare block.

This block looks very much like the numbers equals block, and they share similar functionality, checking to see if the two inputs are equal, but they both also can be modified to check whether one input is larger than the other.  But how can a text input be larger than another?  That's a great question, and the answer is found in the compare text block tooltip:

Returns whether or not the first string is lexicographically <, >, or = the second string depending on which dropdown is selected.

A string a considered lexicographically greater than another if it is alphabetically greater than the other string. Essentially, it would come after it in the dictionary. All uppercase letters are considered smaller or to occur before lowercase letters. cat would be > Cat.

Okay, so that's a strange word, lexicographically.  Lexicography is the art of putting together dictionaries, so we can read this word as if it's saying, "is the first word going to come before the second word in the dictionary".  I did some looking online for lexicographically with regards to coding and it seems that each letter is given a sort of numeric representation within a programming language, like Java for instance.  Think of the alphabet having 26 letters and a=1 b=2...z=26.  So z is the largest letter of them all.  Knowing that, we can use the compare texts block to sort a list of names for us.


This sort uses optimized bubble sort, as Scott shows in the second link above.  I basically took his example and adapted it to work with names instead of numbers; this also doesn't permit for sorting it in reverse, although that's easily accomplished by adding the extra procedure argument in his example.


Next step is to see whether it's possible to associate a score with a name and use bubble sort to sort it either way, keeping the score and name together.

Taifun

unread,
Jun 23, 2014, 9:34:06 AM6/23/14
to mitappinv...@googlegroups.com
great example!
Taifun

Taifun

unread,
Jun 23, 2014, 2:11:12 PM6/23/14
to mitappinv...@googlegroups.com
after having seen your solution, I was thinking, why reinventing the wheel? 
Why not just using a simple JavaScript sort method?
see my solution, which uses the webviewer here https://puravidaapps.com/snippets.php#2sort
Taifun

Scott Ferguson

unread,
Jun 24, 2014, 10:17:21 AM6/24/14
to mitappinv...@googlegroups.com
Hi Taifun. That looks incredibly simple -- but brilliant!
I can't get any output on my tablet in development mode.
I think I will try it using an html document in the media list instead to see if that works.
---
Scott

Scott Ferguson

unread,
Jun 24, 2014, 10:19:06 AM6/24/14
to mitappinv...@googlegroups.com
...and Nice one, Stephen :)
---
Scott

Stephen Zipprich

unread,
Jun 24, 2014, 10:31:42 AM6/24/14
to mitappinv...@googlegroups.com
@Taifun, "there is more than one way to skin a cat." 😀 But in all seriousness, there are a lot of times people are requesting solutions that don't require an Internet connection. This solution is one way to accommodate that.

@Scott, thanks, but you really did all the hard work. Credit where it's due. ☺

Taifun

unread,
Jun 24, 2014, 10:36:27 AM6/24/14
to
I can't get any output on my tablet in development mode.
I tested on Nexus 5 running Android 4.4.3 and it works fine in development mode with the companion app
did you test with my example project?

probably to execute JavaScript in the URL bar of the webviewer does not work on all devices?
if this is the case, a simple html page could do it as workaround, which just prints the result to the page title
we will have to do more tests...

Taifun

Taifun

unread,
Jun 24, 2014, 11:12:39 AM6/24/14
to mitappinv...@googlegroups.com
I now tested on Samsung Galaxy Tab 10.1N running Android 3.2 and can confirm your issue

to solve this, I set the HomeURL to a specific page (e.g. google.com) and now it works there, too, see updated example project https://puravidaapps.com/snippets.php#2sort



Taifun

Taifun

unread,
Jun 24, 2014, 11:18:30 AM6/24/14
to mitappinv...@googlegroups.com
@Stephen: the first solution did not use an internet connection, the JavaScript just was executed in the URL of the webviewer, unfortunately this did not work on the tablet

for the new solution therefore I set the home URL to a specific webpage, but a local HTML page also could do it...

of course there are always more solutions to a problem
here I just want to show an easy way how to include some JavaScript...

Taifun

Scott Ferguson

unread,
Jun 24, 2014, 10:02:28 PM6/24/14
to
I am running Android 4.1.1 on a Monster M7 tablet.
My workaround was to use window.document.title to send the list back to App Inventor where it was retrieved with the WebViewer CurrentPageTitle property.
So that does work for me, thanks.


list.html:

<!DOCTYPE html>
<html>
<body>
<script>

  var list = eval('[' + window.AppInventor.getWebViewString() + ']');
  list.sort();
  window.document.title = list;

</script>
</body>
</html>

---
Scott
Reply all
Reply to author
Forward
0 new messages