//Define an object array
var data = [{ name : "jiang", age: 22 }, { name: "AAAAAAAAAAAAAAA", age: 21 }, { name: "CCCCCCCCCc", age: 25}];
//Define a comparator
function compare( propertyName) {
return function (object1, object2) {
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value2 < value1) {
return -1;
}
else if (value2 > value1) {
return 1;
}
else {
return 0;
}
}
}
//How to use
data.sort(compare("name"));
alert(data[0].name);//jiang
//How to use
Data.sort(compare("age"));
alert(data[0].age);//25s
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn