JS

HOW TO SORT ARRAYS IN JAVASCRIPT

1) BASIC SORT

THE ARRAY var arr = ["Red", "Blue", "Green"];   SORT ASCENDING ORDER Blue, Green, Red arr.sort();   SORT DESCENDING ORDER Red, Green, Blue arr.sort().reverse();

2) SORTING NUMBERS

THE ARRAY var arr = [41, 5, 97];   SORT ASCENDING ORDER 5, 41, 97 arr.sort(function(a, b){ return a - b; });   SORT DESCENDING ORDER 97, 41, 5 arr.sort(function(a, b){ return b - a; });