return function(obj1, obj2) {
var v1 = obj1[prop],
v2 = obj2[prop]
return v1 > v2 ? 1 : v1 < v2 ? -1 : 0 }
}
var arr = [
{ name: 'li', age: 18 },
{ name: 'an', age: 19 },
{ name: 'tian', age: 18 }
]
arr.sort(compare('name')) (3) [{…}, {…}, {…}]
return function(obj1, obj2) {
var v1 = obj1[prop],
v2 = obj2[prop]
return v1 > v2 ? 1 : v1 < v2 ? -1 : 0 }
}
var arr = [
{ name: 'li', age: 18 },
{ name: 'an', age: 19 },
{ name: 'tian', age: 18 }
]
arr.sort(compare('name')) (3) [{…}, {…}, {…}]