Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: AppleParser



That does, would help it was native but what you going to do =)

Thanks

hey... got an easy way to sortOn multiple keys? =)

On 8/24/06, Nate Cook <email@hidden> wrote:
> 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 :.


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Dashboard-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/dashboard-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: AppleParser (From: "Nate Heagy" <email@hidden>)
 >Re: AppleParser (From: John Louch <email@hidden>)
 >Re: Re: AppleParser (From: "Johnny Storm" <email@hidden>)
 >Re: AppleParser (From: Nate Cook <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.