其他分享
首页 > 其他分享> > 包含两个对象的数组排序

包含两个对象的数组排序

作者:互联网

 1 var data =[{name:19,age:28},{name:30,age:29}]
 2 function createComparisonFunction(propertyName){
 3     return function(object1,object2){
 4         var value1 = object1[propertyName];
 5         var value2 = object2[propertyName];
 6         if(value1<value2){
 7             return -1;
 8         }
 9         else if(value1>value2){
10             return -1;
11         }
12         else{
13             return 0;
14         }
15     }
16 }
17 data.sort(createComparisonFunction("name"));
18 console.log(data[0]); //{name: 30, age: 29}
19 data.sort(createComparisonFunction("age"));
20 console.log(data[0]); //{name: 19, age: 28}

 

标签:return,name,包含,age,数组,var,排序,data,propertyName
来源: https://www.cnblogs.com/wss198909/p/16589013.html