Skip to content

Instantly share code, notes, and snippets.

@panicbus
Created October 20, 2016 18:40
Show Gist options
  • Save panicbus/7a36ace2650cd8471dabbf08c1030fe5 to your computer and use it in GitHub Desktop.
Save panicbus/7a36ace2650cd8471dabbf08c1030fe5 to your computer and use it in GitHub Desktop.
Neat JS sort function
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