en.javascript.info/1-js/5-functions-closures/4-closures-usage/4-sort-by-field/solution.md
2015-03-10 12:36:58 +03:00

517 B

//+ run
var users = [{
  name: "Вася",
  surname: 'Иванов',
  age: 20
}, {
  name: "Петя",
  surname: 'Чапаев',
  age: 25
}, {
  name: "Маша",
  surname: 'Медведева',
  age: 18
}];

*!*
function byField(field) {
    return function(a, b) {
      return a[field] > b[field] ? 1 : -1;
    }
  }
*/!*

users.sort(byField('name'));
users.forEach(function(user) {
  alert( user.name );
});

users.sort(byField('age'));
users.forEach(function(user) {
  alert( user.name );
});