Created
February 12, 2009 14:52
-
-
Save joshuaclayton/62652 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uniq: function() { | |
var resultArray = this.clone().sort(function(a,b) { if(a == b || JSON.stringify(a) == JSON.stringify(b)) { return 0; } if(a > b || JSON.stringify(a) > JSON.stringify(b)) { return 1; } return -1; }), | |
clone = this.clone(), | |
first = 0, | |
last = resultArray.length, | |
sorted = arguments[0] || false; | |
var unsort = function(sortedArray) { | |
var result = []; | |
var parsedSortedArray = $.map(sortedArray, function(i) { return JSON.stringify(i); }), | |
parsedClone = $.map(clone, function(i) { return JSON.stringify(i); }); | |
for(var i = 0, len = parsedClone.length; i < len; i++) { | |
if(!parsedSortedArray.length) { break; } | |
var current = parsedClone[i], | |
sortedIndex = parsedSortedArray.indexOf(current); | |
if(sortedIndex != -1) { | |
result.push(clone[i]); | |
parsedSortedArray.splice(sortedIndex, 1); | |
} | |
} | |
return result; | |
}; | |
for(var alt; (alt = first) != last && ++first != last; ) { | |
if(resultArray[alt] === resultArray[first] || JSON.stringify(resultArray[alt]) == JSON.stringify(resultArray[first])) { | |
for(; ++first != last;) { | |
if (resultArray[alt] !== resultArray[first] && JSON.stringify(resultArray[alt]) != JSON.stringify(resultArray[first])) { resultArray[++alt] = resultArray[first]; } | |
} | |
++alt; | |
resultArray.length = alt; | |
return sorted ? resultArray : unsort(resultArray); | |
} | |
} | |
return sorted ? resultArray : unsort(resultArray); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment