But sorting actually depends on array element type, thus solution can not be abstract enough. It is bad suprise for me that anonymous functions are bad. I like them very much for Array methods like filter(), map(), some(), any() and forEach(). So if I create private methods instead, it would be faster, right?
On Fri, Feb 22, 2008 at 11:31 PM, Gordon Smith <[EMAIL PROTECTED]> wrote: > Are you trying to compare two Arrays as sets, where [ 1, 3, 2, 1 ] and > [ 1, 1, 2, 3 ] are considered the same? > > If so, I would sort() them -- which I would expect to take only O(n ln n) > -- and then compare them element by element until you found a mismatch or > reached the end. > > Avoid using anonymous functions. My understanding is that they don't > perform as well as class methods because they require an activation frame. > > Gordon Smith > Adobe Flex SDK Team > > ------------------------------ > *From:* flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] *On > Behalf Of *Maciek Sakrejda > *Sent:* Friday, February 22, 2008 9:23 AM > *To:* flexcoders@yahoogroups.com > *Subject:* Re: [flexcoders] What is the best way to compare two > arrayselement by element ignoring the order? > > Extra points for higher-order function, although that is going to be > O(n^2) (unless Array.indexOf() has a weird implementation). If you've > got really, really big arrays (or are doing this in a tight loop) and > you have O(n) memory to spare, you could consider building a hashmap of > the values in Array a, and then walking Array b and checking if each > element is in the hashmap: > > var differs:Boolean = (a.length != b.length); > if (!differs) { > var aContents = new Object(); > a.forEach(function(item:Object, index:int, array:Array):void { > aContents[item] = true; > }); > differs = b.some(function(item:Object, index:int, array:Array):Boolean > { > return aContents[item] == null; > }); > } > > For small-ish arrays, though, I would expect your solution to be faster > than mine (I won't define 'small-ish', since I would honestly be pulling > a number out of my--err, out of thin air). > > Also, neither your solution nor mine handles the case where the same > item is in one array twice (yours fails for dupes in a and mine for > dupes in b). > > -- > Maciek Sakrejda > Truviso, Inc. > http://www.truviso.com > > -----Original Message----- > From: Sergey Kovalyov <[EMAIL PROTECTED]<skovalyov.flexcoders%40gmail.com> > > > Reply-To: flexcoders@yahoogroups.com <flexcoders%40yahoogroups.com> > To: flexcoders@yahoogroups.com <flexcoders%40yahoogroups.com> > Subject: [flexcoders] What is the best way to compare two arrays element > by element ignoring the order? > Date: Fri, 22 Feb 2008 15:37:56 +0200 > > What is the best way to compare two arrays element by element ignoring > the order? My solution: > > var differs : Boolean = > (a.length != b.length) || > a.some( > function(item : Object, index : int, array : Array) : Boolean { > return (b.indexOf(item) == -1); > }); > > May be the better solution exists? > > >