Created
October 20, 2016 18:40
-
-
Save panicbus/7a36ace2650cd8471dabbf08c1030fe5 to your computer and use it in GitHub Desktop.
Neat JS sort function
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
| Array.prototype.nSort = nSort; | |
| // [].prototype.nSort works too | |
| function nSort(){ | |
| return this.sort( | |
| function numSort(n1, n2){ | |
| if (n1 < n2) return -1; | |
| if (n1 > n2) return 1; | |
| return 0; | |
| } | |
| ); | |
| } | |
| var numbers = []; | |
| numbers[0] = 20; | |
| numbers[1] = 22; | |
| numbers[2] = 21; | |
| numbers[3] = 19; | |
| console.log(numbers.nSort()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment