Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradoyler/9128629 to your computer and use it in GitHub Desktop.
Save bradoyler/9128629 to your computer and use it in GitHub Desktop.
JS console lesson : sorting numbers in Array : the functional nature of js
// figure out how this works and why it's important
// array
var arr =[2,3,5,5,1,99,22,44];
// sort ascending
arr.sort(function(n1,n2){
return n1 - n2;
}); //output: [1, 2, 3, 5, 22, 44, 99]
console.log(arr);
// descending sort
arr.sort(function(n1,n2){
return n2 - n1;
}) //output: [99, 44, 22, 5, 3, 2, 1]
console.log(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment