Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was also looking for this today and found: <a href="http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256BFB0077DFFD" rel="nofollow noreferrer">http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256BFB0077DFFD</a></p> <p>Don't know if that's a good solution though they do mention some performance considerations taken into account.</p> <p>I like the idea of a jQuery helper method. @David I'd rather see your compare method to work like:</p> <pre><code>jQuery.compare(a, b) </code></pre> <p>I doesn't make sense to me to be doing:</p> <pre><code>$(a).compare(b) </code></pre> <p>where a and b are arrays. Normally when you <em>$(something)</em> you'd be passing a selector string to work with DOM elements.</p> <p>Also regarding sorting and 'caching' the sorted arrays:</p> <ul> <li>I don't think sorting once at the start of the method instead of every time through the loop is 'caching'. The sort will still happen every time you call compare(b). That's just semantics, but...</li> <li><em>for (var i = 0; t[i]; i++) {</em> ...this loop finishes early if your <strong>t</strong> array contains a false value in it somewhere, so <em>$([1, 2, 3, 4]).compare([1, false, 2, 3])</em> returns <em>true</em>!</li> <li>More importantly the array sort() method sorts the array <em>in place</em>, so doing <strong>var b = t.sort()</strong> ...doesn't create a sorted copy of the original array, it sorts the <em>original</em> array and also assigns a <em>reference</em> to it in <strong>b</strong>. I don't think the compare method should have side-effects.</li> </ul> <p>It seems what we need to do is to copy the arrays before working on them. The best answer I could find for how to do that in a jQuery way was by none other than John Resig here on SO! <a href="https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object/122704#122704">What is the most efficient way to deep clone an object in JavaScript?</a> <em>(see comments on his answer for the array version of the object cloning recipe)</em></p> <p>In which case I think the code for it would be:</p> <pre><code>jQuery.extend({ compare: function (arrayA, arrayB) { if (arrayA.length != arrayB.length) { return false; } // sort modifies original array // (which are passed by reference to our method!) // so clone the arrays before sorting var a = jQuery.extend(true, [], arrayA); var b = jQuery.extend(true, [], arrayB); a.sort(); b.sort(); for (var i = 0, l = a.length; i &lt; l; i++) { if (a[i] !== b[i]) { return false; } } return true; } }); var a = [1, 2, 3]; var b = [2, 3, 4]; var c = [3, 4, 2]; jQuery.compare(a, b); // false jQuery.compare(b, c); // true // c is still unsorted [3, 4, 2] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload