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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-03
  • 2021-07-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
猜你喜欢
  • 2022-12-23
  • 2021-09-23
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
相关资源
相似解决方案