function byField (field) {
return function (a, b) {
return a[field] > b[field] ? 1 : -1;
};
}
var users = [{
name: "Вася",
surname: 'Иванов',
age: 20
}, {
name: "Петя",
surname: 'Чапаев',
age: 25
}, {
name: "Маша",
surname: 'Медведева',
age: 18
}];
byField();
users.sort(byField('age'));
users.forEach(function(user) {
console.log( user.age);
});