<< back to BigData
The Sort Command
You can sort lists: - up or down (ascending or descending) - textually, numerically or by date/time
- by words, items or lines (by any part of the line)
A. Sorting the Names - Alphabetically We have seen how the find works and stepped through all the names that begin with "Sam"
That helps but it would be nice if all the names were in order.
Lets put them in alphabetical order by typing: Sort field "names" in the MessageBox
That looks better, now let's so the find again: Find "Sam" in field "names"
That looks good, because we now have all the names beginning with "Sam" together.
looking at the data, for this year there were
15 Females named Sam 476 Males named Sam
B. Sorting the Names - by Popularity
But we want to know the popularity or the rank of the name we find. The third field of our list containg the count of people with that name.
e.g. Samantha,F,20191 - means the name Samantha is Female and 20,191 girls were named that.
So if we sort the names list by the third field, we will have the list sorted by popularity.
We want to do the following: sort it by item 3 of each line sort it numerically not alphabetically sort it in reverse (or descending) order - largest number at the top down to smallest number at the bottom
So the sort line now becomes:
sort lines of field "names" descending numeric by item 3 of each
*** WARNING - Sorting a field on the screen may HANG the program. ***
Save your program NOW !!!
If this hangs, we can come back to it...
Let us try that in the Message Box and see how it turns out:
If we do a "find" on "Sam" we now get this:
B. Important Notes - The Sort Command
note 1: Sort in Variables NOT fields
It is a good practice to read a list into a variable first. Work with your list in that variable. Change it, sort it, rearrange it, then put it into a field on the screen/card.
Sorting, especially, is slow when done on the screen because as the computer rearranges the items, it also has to worry about screen size, resolution, field size, etc. All those slow it down, sometimes to a crawl.
As any gamer knows, you do not move, sort or change things on the screen. It is too slow and may hang the system.
The Solution...
Put the data into a variable, sort it there, then put it back in the field
The code:
put field "names" into x sort the lines of x numeric descending by item 3 of each
put x into field "names"
C. Finishing our Program Add 2 buttons to the screen, so that we can change the order between alphabetic and popularity
Name one "Sort Alphabetically" and the other "Sort by Popularity",
On the "Sort Alphabetically" button, add the following code:
on mouseUp
put field "names" into x
sort lines of x by item 1 of each
put x into field "names"
end mouseUp
On the "Sort by Popularity" button, add the following code:
on mouseUp
put field "names" into x
sort lines of x numeric descending by item 3 of each
put x into field "names"
end mouseUp
|
|