Javascript Array sortBy(p, type)

Description

Javascript Array sortBy(p, type)


Array.prototype.sortBy = function(p, type) {
  if(type == undefined) type = "asc";

  if(type == "desc"){
    return this.slice(0).sort(function(a,b) {
      return (a[p] < b[p]) ? 1 : (a[p] > b[p]) ? -1 : 0;
    });        //from www. j  a  v  a 2 s.  c  o  m
  } else {
    return this.slice(0).sort(function(a,b) {
      return (a[p] > b[p]) ? 1 : (a[p] < b[p]) ? -1 : 0;
    });  
  }  
};



PreviousNext

Related