实例1:
function creatComparisonFunction(propertyName){
return function(object1,object2){
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value1<value2)
{
return -1;
}else if (value1 > value2)
{
return 1;
}else {
return 0;
}
}
}
var date =[
{name:'Zachary',age:28},
{name:'Nicholas',age:29}
];
date.sort(creatComparisonFunction('name'));
console.log(date[0].name);
date.sort(creatComparisonFunction('age'));
console.log(date[0].name);
实例2:
function creatComparisonFunction(propertyName){
var i=0;
return function(){
var value1 = arguments[i][propertyName];
var value2 = arguments[i+1][propertyName];
if (value1<value2)
{
return -1;
}else if (value1 > value2)
{
return 1;
}else {
return 0;
}
}
}
var date =[
{name:'Zachary',age:28},
{name:'Nicholas',age:29}
];
date.sort(creatComparisonFunction('name'));
console.log(date[0].name);
date.sort(creatComparisonFunction('age'));
console.log(date[0].name);