Javascript Array sort(comp)

Description

Javascript Array sort(comp)



Array.prototype.sort = function (comp) {
 var arr = this;/*from   w  w  w  . jav  a  2s  .com*/
      for (var i = 1; i < arr.length; i++) {
        var tmp = arr[i],
            j = i;
        while (comp(arr[j - 1] , tmp ) > 0 ) {
          arr[j] = arr[j - 1];
          --j;
        }
        arr[j] = tmp;
      }
    
      return arr;
    };

Javascript Array sort(comp)

Array.prototype.sort = function(comp) {

      var min, tmp , arr = this;
      for (var i = 0; i < arr.length; i++) {
        min = i;/*from ww  w  .  j  a  v a2 s.  c o  m*/
        for (var j = i + 1; j < arr.length; j++) {
          if ( comp(arr[min], arr[j]) > 0 ) {
            min = j;
          }
        }
        if (min != i) {
          tmp = arr[i];
          arr[i] = arr[min];
          arr[min] = tmp;
        }
      }
    
      return arr;
      }



PreviousNext

Related