> Macromedia flash support the ability to sortOn elements in a
> multidimensional array. Support for key sorting either through XML
> nodes or JavaScript multidimensional arrays would be great.
>
> ie:
>
> var items = new Array();
> items.push({title: "Star Wars", year: "1977"});
> items.push({title: "Empire Strikes Back", year: "1980"});
> items.push({title: "Return of the Jedi", year: "1983"});
>
> items.sortOn("title");
> items.sortOn("year");
>
> Unless something like that already exists and I just haven't found
> it yet.
Javascript lets you pass a comparison function to the Array.sort
function, so you can do specific types of sorting by writing a
function that will determine which item should go where in the list.
This function will sort based on the title:
function sortTitle(a, b) {
if (a.title > b.title) return 1;
else if (a.title < b.title) return -1;
else return 0;
}
items.sort(sortTitle);
To emulate ActionScript's Array.sortOn method, you can add it into
the Array prototype, like so:
Array.prototype.sortOn = function(key) {
return this.sort(function (a,b) {
if (a[key] > b[key]) return 1;
else if (a[key] < b[key]) return -1;
else return 0;
});
}
items.sortOn('title');
items.sortOn('year');
Hope that helps --
Nate
.: Nate Cook : 312 259 0087 : email@hidden : www.natecook.com :.