Created
August 15, 2014 00:00
-
-
Save alexhawkins/4d01f9e3631598c6db43 to your computer and use it in GitHub Desktop.
A Unique Sorting Function in JS.
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
| 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