数组对象按字母顺序或数字大小排序

 1  <html>
 2   <body>
 3  
 4   <script type="text/javascript">
 5   function compare(property){//数字字母排序
 6       return function(a,b){
 7           var value1 = a[property];
 8           var value2 = b[property];
 9          return (value1 >value2 ? 1 : -1);
10      };
11  }
12  function compareLetter(property){//中文首字母排序
13       return function(a,b){
14                var param1 = a[property];
15                var param2 = b[property];
16                   return param1.localeCompare(param2);  //output:之,家,本,脚
17              };
18            }
19 
20  var arr = [
21      {name:"快手抢红包",age:8,birthDay:'2017-10-08',letter:"zarte"},
22      {name:"消灭六边形",age:9,birthDay:'2018-10-08',letter:"aree"},
23      {name:"啊啊",age:18,birthDay:'2017-11-08',letter:"ooo"},
24      {name:"呀呀",age:8,birthDay:'2016-10-08',letter:"body"},
25      {name:'手机助手',age:8,birthDay:'2018-01-08',letter:"ledy"},
26      {name:'宠物连连看',age:8,birthDay:'2017-05-08',letter:"miss"},
27      {name:'一笔画',age:8,birthDay:'2017-07-08',letter:"girl"}
28  ];
29  
30  console.log(arr.sort(compareLetter('name')));//中文首字母排序
31  console.log(arr.sort(compare('letter')));//字母升序
32  console.log(arr.reverse(compare('letter')));//字母降序序
33  console.log(arr.sort(compare('birthDay')));//时间升序
34 
35  </script>
36  
37  </body>
38  </html>
39   
原文地址:https://www.cnblogs.com/hyywaq/p/8295870.html