function Human(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
this.speak = function() {
return "My name is " + this.name + ". I'm " + this.age + " year old, and I can speak";
}
}
//создание 4 экземпляров Human
var john = new Human('John', 22, 'male');
var tom = new Human('Tom', 25, 'male');
var alice = new Human('Alice', 21, 'female');
var betty = new Human('Betty', 23, 'female');
//создание массива с объектами
var arr = [john, tom, alice, betty];
// функция сравнения по возрасту
function compareAge(a,b) {
if (a.age > b.age) {
return 1;
}
if (a.age < b.age) {
return -1;
}
}
//сортировка элементов массива arr по свойству age
arr.sort(compareAge);
//вывод в консоль отсортированного массива
console.log(arr);