现在的位置: 首页 > 综合 > 正文

Javascript 自定义数组排序

2012年08月12日 ⁄ 综合 ⁄ 共 470字 ⁄ 字号 评论关闭
  <html>
 <head>
  <title>测试</title>
  <meta http-equiv="content-type" content="text/html;chaset=UTF-8"/>
 </head>
 
 <body>
  <script>
   
   function createComparisonFunction(propertyName){
   
    return function(object1,object2){
     var value1 = object1[propertyName];
     var value2 = object2[propertyName];
     
     if(value1 < value2){
      return -1;
     } else if(value1 > value2 ){
      return 1;
     } else {
      return 0;
     }
    };
   }
   var arr = [{name:"Zachar",age:28},{name:"Eye",age:18},{name:"Eye",age:18}];
   arr.sort(createComparisonFunction("age"));
    
  </script>
 </body>
</html>

抱歉!评论已关闭.