Simon Palmer’s blog

January 19, 2009

Sorting flex Arrays by Date

Filed under: code, Flex — simonpalmer @ 2:11 pm

I have been wanting to figure out a reliable way of doing this for a while, and suddenly the necessity arose for me to do it, so I have a solution. As far as I can tell if you have a date field in an object and an array of those objects, then the results of sorting the array are indeterminate.


public class myClass
{
    public var some_date: Date;
}

var myArray:Array = new Array();
myArray.push(new myClass);

myArray.sortOn("some_date");
myArray.sortOn("some_date", Array.NUMERIC);

The above code (so long as the some_date field was populated) never gave me reliable result for the sort order. I googled in vain for a solution and found a couple, but was never really happy with them. I had a flash of inspiration this morning which is really very obvious now I have done it, but is a nice elegant way of handling this situation.

Basically I add a public getter for a new field which just returns the Date objects time value as a Number, e.g.

public class myClass
{
    public var some_date: Date;
    [Transient] public function get some_date_time():Number
    {
        return some_date.time;
    }
}

var myArray:Array = new Array();
myArray.push(new myClass);
myArray.sortOn("some_date_time", Array.NUMERIC);

This works a treat and gives me reliable sorting results every time.

In case you are wondering why I use [Transient] it is because I am using RPC and remote objects for the connection back to my server and my classes are generally [Managed]. Without the (undocumented) [Transient] metadata some_date_time would be treated as a legitimate field and an attempt would be made to serialise it back to the server across the RPC boundary. This causes a warning to be thrown in the Flex server framework code which clogs up my logs and feels unprofessional.

October 16, 2008

Adventures in Media Planning

Filed under: Flex — simonpalmer @ 8:31 pm

I’ve been recently working on an algorithm which sorts through a large list of possible advertising media and suggests a set of suitable candidates for inclusion in a campaign.  I’m doing this in conjunction with a company in Hong Kong called AdvertisingPlaza.com who have created a media buying market place.  They act as broker and middle-man between advertisers and the ad providers.  The media they have on their books if of a wide range, from TV ads, to billboards to newspapers and magazines and online ads.

I’m quite excited…

The revolution is that the media buyers being targetted are not media buyers at all but regular business people buying ad space.  This is something I have done a little of myself in the past and I struggled to know how and where to spend my money.  I never had the budget or ongoing need to merit using an agency to plan a campaign for me, so I guessed.  On reflection I probably got it partly right, but that means I also got it mostly wrong.

It turns out that this is not an uncommon scenario, so what we did (we being me and a brainy friend) is dream up a means of allowing a novice user to tell us about the following things:

  • what the demographic of the target audience is
  • what the intention of the campaign is
  • how much money they have to spend

and we then look through the list of media that are available and come up with a suggestion for them which matches who they are trying to reach, what they are trying to do and how much they want to spend.

We have them tell us these things by filling in an interactive online form – with sliders! – in a web page on the AdvertisingPlaza.com site (oh, btw, if you go looking there right now you won’t find it because we’re still beta testing with selected customers).  They press the magic button and in the background our algorithm chugs away and they are presented with a list that they can either just buy straight away, add to or change quantities, or contact the sales team to start haggling about price.

The clever bit is how we decide what we should include.  It turns out this is actually quite a complex problem, not least because the advertising media tend to overlap – by which I mean that a billboard and a newspaper and a TV ad will have some audience in common.  What’s more they individually have very different reaches and different impacts on the viewer.

Then there’s the question of cost.  It’s possible to come up with the perfect media campaign, but how do you maximise the effect and still stay within budget?

These were the problems that we set about tackling and we have come up with a very novel way of doing it which doesn’t mean a huge computational cost as well.  So how does it work?  Well, that would be telling.  Just as soon as we have some solid commercial success I’ll spill the beans, but until then it has to remain top secret.  Suffice to say it has Flex and Python in it…

January 15, 2008

Perhaps humans are not all bad…

Filed under: Flex — simonpalmer @ 10:36 pm

One of the things I love about the Flex community in its current form is the generosity of spirit shown by the contributors.  Few things do more to restore my faith in human nature than the goodwill and patience that is prevalent on the forums and code sharing resources that belong to the Flex community.

Adobe for their part have gone a long way to encourage and develop this ecosystem of developers by allotting time – or at least permitting time – to be spent by senior members of their development teams posting replies to the often terse, sometimes downright stupid, occasionally offensive, but mostly honest and open queries from members of the public who post on forums like Flexcoders.

This is a nice departure from the normal grasping and unenlightened attitude prevalent in corporate software development.  It has benefitted me and my startup enormously.  I even find myself pledging a small amount of my own time to contribute back to these groups, trying to help  in the same way in which I was generously helped when I was a newbie and didn’t know my as3 from my elbow.

I say well done Adobe and thank you flexcoders!

Blog at WordPress.com.