<< back to BigDataSorting In Different Ways Now we may want to look at the data in different ways - We have 3 fields - and want to sort them differently 1 - Names - sort the names alphabetically
2 - Ages - sort the counties by their median ages, to see which counties have the youngest/oldest residents
3 - Househole income - sort the counties from richest to poorest
We can do that by adding 3 buttons:
1 - "Sort Names" with the code: on mouseUp
sort field "mydata"
end mouseUp
2 - "Sort by Age" with the code:
on mouseUp
sort field "mydata" by item 2 of each
end mouseUp
3 - "Sort by Income" with the code:
on mouseUp sort field "people" numeric by item 3 of each end mouseUp
The first two work but the 3rd one doesn't. Look a the 3rd field. It isn't sorted correctly. The dollar sign ($) gets in the way. The $ is not a number so it does not get sorted.
So we have to eliminate it from the number.
We can do that when we load it: So we need to make a change to the "Load File" script:
on mouseUp answer file "Pick a file to use" put it into myfile put url ("file:" & myfile) into temp
put empty into field "mydata" repeat for each line x in temp
put item 1 of x & " , " & item 8 of x & " , " & char 2 to 9 of item 10 of x & return after field "mydata" end repeat end mouseUp
This skips over the dollar sign which is char 1 (character 1 of the number).
Now the numeric sort works
Keeping the Heading at the Top when Changing the Sort Order
When we sort, the header gets sorted too. If we want to keep it at the top, we save it in a variable Then we remove it from the data, sort the data, then put the heading back at the top
On the Sort button, add the following:
on mouseUp put line 1 of field "mydata" into temp
delete line 1 of field "mydata" sort field "mydata" by item 2 of each put ( temp & return) before field "mydata"
end mouseUp
|