javascript 排序的妙用

function creatCompare(propertyName){

return function (ob1,ob2){

var val1=ob1[propertyName];

var val2=ob2[propertyName];

  if(val1<val2){

  return -1}

  else if (val1>val2){

  return 1 }

  else {

   return 0}

  };

}

var data=[{name: 'aaaaa',age:28},{name:'bbbb',age:30}]

data.sort(creatCompare('name'))//按照名字排序

data.sort(creatCompare('age'))//按照年龄排序

简单吧!!

posted @ 2011-06-13 19:41  chalbo  阅读(204)  评论(0编辑  收藏  举报