Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Created August 15, 2014 00:00
Show Gist options
  • Select an option

  • Save alexhawkins/4d01f9e3631598c6db43 to your computer and use it in GitHub Desktop.

Select an option

Save alexhawkins/4d01f9e3631598c6db43 to your computer and use it in GitHub Desktop.
A Unique Sorting Function in JS.
arr = [7, 7, 9, 2, 45, 6, 34, 43, 2, 6, 8, 2, 4, 100, 100, 100, 23, 34, 4];
var uniq = function(arr) {
return arr.filter(function(value, index, self) {
// only returns uniq numbers. A duplicate will yield a different indexOf
return self.indexOf(value) === index;
}).sort(function(x, y) {
return x - y;
});
};
console.log(uniq(arr)); // [ 2, 4, 6, 7, 8, 9, 23, 34, 43, 45, 100 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment