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 13 14 var arr = [ 15 {name:‘zopp‘,age:0}, 16 {name:‘aopp‘,age:9}, 17 {name:‘gpp‘,age:18}, 18 {name:‘yjj‘,age:8} 19 ]; 20 21 console.log(compare(‘name‘)); 22 console.log(arr.sort(compare(‘name‘))); 23 24 </script> 25 26 </body> 27 </html> 28