Javascript Array natsort(direction)

Description

Javascript Array natsort(direction)


/**/*from w  w  w.  j a  va 2s  .  co  m*/

var myArray = [1,10,2,3,4,5];
var answer = myArray.natsort();
//answer = [1,2,3,4,5,10];
*/
Array.prototype.natsort = function(direction){
 direction = (direction ==-1) ? -1 : 1;
 if(direction == -1){
  this.sort(function(a,b){return (b-a);});
 } else {
  this.sort(function(a,b){return (a-b);});
 }
 return this;
};



PreviousNext

Related