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

 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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值