Sorting in AS2

One of the things you can’t get away from in data visualization is sorting. It’s important for drawing the visualizations. It’s also important for user expectations. All users are used to an OS where you can sort. They will expect the same for a visualization. Sorting in AS1 was slow. I ended up writing includes for all my sorting. AS2 compiles down to AS1 byte code, so most performance is identical (although Player 7 is a lot faster than 6). But, the one area with really significant speed increase was the sorting.

For code I use all the time, I usually end up writing a wrapper around the base class or method. When I wrote this sorting wrapper, I found one thing that was really useful – using options 17 and 19 will sort numbers, strings, and booleans. (Dates are the exception and you need to convert them to millisecond values (getTime()) to sort them as numbers.) So, you could secondary sort using properties of different type.

What am I talking about? First, a few definitions…

sortOn – sorting an array of objects by a property – E.g., a person could have many properties – age, name, etc. To sort by name, you would use: people.sortOn(“name”)

Secondary sorting – sorting by more than one property. E.g., sorting all baseball players by team, then name, would result in a list of players grouped by team. And, within each team, they would be sorted alphabetically.

Sort option – how you want to sort alphabetical, numeric, reverse numeric, etc.

In Player 7 you can secondary sort, but, all sorting needs to use the same option (Player 8 supports multiple options). However, since options 17 and 19 will correctly sort both numbers and strings (17 and 19 are case insensitive string sorts – change to 16 and 18 for case sensitive), you can correctly sort e.g., name then age. (Always use numbers for option codes btw – NUMERIC is slower than 16).

My tests showed that using option 17 or 19 is just as fast as other sorts. The one exception is passing no option, which is the fastest, but, will only sort as case-insensitive string.

One more thing – I always use sortOn and not sort. I believe sort is slower (or at least it used to be). But more importantly, it always seems to make more sense to have your data structured as arrays of objects, as you almost always need to have more than one property for a piece of data.

Here’s how to use…

var rec_array:Array = new Array();
rec_array.push({name:"john", city:"omaha", zip:68144});
rec_array.push({name:"john", city:"kansas city", zip:72345});
rec_array.push({name:"bob", city:"omaha", zip:94010});
rec_array.push({name:"bob", city:"omaha", zip:9});
ArrayMethods.smartSortOn(rec_array, ['name', 'city','zip']);

And here’s the code and sample.

Hello World and Happy Birthday

My first blog entry is on my wife Adrienne’s birthday. Love, birth, Flash, data visualization – all in one post – auspicious.

Why another Flash blog? Well, this one’s going to be on data visualization – basically fancy interactive charts and graphs made with Flash.

Why charts and graphs? A few reasons…

  1. Data visualization can make the world a better place. Really.
  2. I spoke at the MAX conference this year and people expressed interest in this type of work. That interest is what finally got me blogging. Thanks.
  3. The most interesting thing going on right now in computers is Web2.0. Currently, most of the interesting visualization work is being done using Ajax. I think (hope) Flash will play a bigger role in visualizing Web2.0 data. Ajax, as great as it is, can only take you so far.

What will be here? Links to Flash data sites, ActionScript code, thoughts on data design, some Web2.0.

Any guidelines? Only links to Flash data work. For Ajax stuff, check out Programmable Web’s huge list of mashups.

Why forest and the trees? Good visualizations allow users to see both overviews and and specific details of data sets. Great ones allow you to move seemlessly between the two (think Google Earth).

What about Flex? Flex is Flash. It’s included. I like Flex.

Coming soon… a theme, better link organization.

Happy Birthday Adrienne!!!