Barefoot Development

Flash sortOn coolness ...

It's always fun to discover nice little surprises in function lists. I had such an experience when I recently found the sortOn() function within Flash's Array class.

I come from a Java background, and Java's Collections API includes very sophisticated hooks to sort objects in any number of ways. I didn't expect to find that in ActionScript. So, when I had a list of objects that I needed to sort, I was pleasantly surprised to find sortOn(), which sorts an Array of objects.

I frequently create value objects to represent things in Flash applications. In one application, I'm placing a number of animals on the stage. The environment is kinda 3D, so animals that have a lower Y axis value are farther away in the distance, while higher Y values are closer to the viewer. The Y axis value is randomly determined, so the order is not pre-determined.

Here's a simplified version of the Animal value object with only the properties relevant to this post:

class Animal
{
var initX:Number;
var initY:Number;
var initScale:Number;
var animalType:String;
var animalName:String;
// more props

function init():Void
{
// Init stuff ...
}
}

The application reads user-defined animals from the server, and randomly places these animals on the stage. I keep all the Animal objects in an array for later use.

So, I knew that in Java I could sort these Animal objects by implementing the Comparable interface. I was pretty sure I couldn't write a Comparator in ActionScript. But, I decided to check out the Array object just in case.

Then, I found the sortOn() function. Its whole purpose is to let you sort arrays of objects. It has several flavors which you can read about at the link above, but the one relevant to this lets you pass in one or more fields (object properties), and an optional sort type parameter.

Here's an example:

var animals:Array = loadAnimals();
animals.sortOn(["initY"], Array.NUMERIC);

Since initY is a property of every object in the animals array, the sortOn() function sorts the objects on that field. By default, sortOn() sorts in ASCII order, which is fine for strings, but bad for numbers since 100 would be sorted before 99. So, ActionScript offers options like the Array.NUMERIC constant shown above, which tells sortOn() to sort the properties as numbers.

So, thanks ActionScript team for putting that in there -- it was a nice surprise!

Doug Smith, Senior Developer, Barefoot

Labels:

2 comments

  1. Blogger sink sink socks said:  

    , the fulfillment of the wishto sleep is quite compatible with the retention of a certain amount ofattention in porno ohne tabu a given direction.

  2. Blogger milf said:  

    black mold exposureblack mold symptoms of exposurewrought iron garden gatesiron garden gates find them herefine thin hair hairstylessearch hair styles for fine thin hairnight vision binocularsbuy night vision binocularslipitor reactionslipitor allergic reactionsluxury beach resort in the philippines

    afordable beach resorts in the philippineshomeopathy for eczema.baby eczema.save big with great mineral makeup bargainsmineral makeup wholesalersprodam iphone Apple prodam iphone prahacect iphone manualmanual for P 168 iphonefero 52 binocularsnight vision Fero 52 binocularsThe best night vision binoculars here

    night vision binoculars bargainsfree photo albums computer programsfree software to make photo albumsfree tax formsprintable tax forms for free craftmatic air bedcraftmatic air bed adjustable info hereboyd air bedboyd night air bed lowest pricefind air beds in wisconsinbest air beds in wisconsincloud air beds

    best cloud inflatable air bedssealy air beds portableportables air bedsrv luggage racksaluminum made rv luggage racksair bed raisedbest form raised air bedsaircraft support equipmentsbest support equipments for aircraftsbed air informercialsbest informercials bed airmattress sized air beds

    bestair bed mattress antique doorknobsantique doorknob identification tipsdvd player troubleshootingtroubleshooting with the dvd playerflat panel television lcd vs plasmaflat panel lcd television versus plasma pic the bestThe causes of economic recessionwhat are the causes of economic recessionadjustable bed air foam The best bed air foam

    hoof prints antique equestrian printsantique hoof prints equestrian printsBuy air bedadjustablebuy the best adjustable air bedsair beds canadian storesCanadian stores for air beds

    migraine causemigraine treatments floridaflorida headache clinicdrying dessicantair drying dessicantdessicant air dryerpediatric asthmaasthma specialistasthma children specialistcarpet cleaning dallas txcarpet cleaners dallascarpet cleaning dallas

Post a Comment

« Home