包含两个对象的数组排序
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}
posted on 2022-08-15 17:22 caicai2015 阅读(28) 评论(0) 编辑 收藏 举报