Sorting of an Array in Javascript

  1. // For the sorting of an Array of Numbers
  2. var tempArr = [2,4,11,3,9,18,15,10];
  3. tempArr.sort(function(a,b){
  4.    return a-b;
  5. });  
  6. Output :: [2,3,4,9,10,11,15,18]; 

  7. // For the sorting of an Array of Characters;
  8. var tempArr2 = ['a','c','g','s','d','b','e'];
  9. tempArr2.sort();
    Output :: ["a", "b", "c", "d", "e", "g", "s"]
     
    So in there 2 examples this can be easily visible for sorting of an Array of numbers we need a call back function for sorting but sorting of characters is very straight forward just by calling .sort() method with the array variable.