Javascript Sort

JavaScript Sort()

Here's how to use the sort() (and also the reverse())

USAGE: Copy and paste the code samples below into the Javascript console in your browser, then simply hit return on your keyboard to see the code in action.

//sort actually sorts the values in an array, typical usage is to assign 
//incoming values as "a" and "b"
const ages = [33,12,20,16,5,54,21,44,61,13,15,45,25,64,32];
const agesSort = ages.sort((a,b) => a-b);
console.log(agesSort);

//reverse works nicely too
const agesRev = ages.reverse(); 
console.log(agesRev);


Link your website to this page! Copy and paste the URL below:
http://www.cfsnap.com/javascript/javascript-sort/
Copyright 2019. All Rights Reserved.