I have an array that contains multiple objects, which i'm trying to add to another larger array with more objects in it. I'm wondering how I can automatically assign their position based on a property that each object shares. Example:
function person(name, age) {
this.name = name;
this.age = age;
}
function pet(name, age) {
this.name = name;
this.age = age;
}
var person1 = new person("Amanda", 25);
var person2 = new person("Jack", 29);
var pet1 = new pet("Fluffy", 4);
var pet2 = new pet("Spaz", 5);
personArray = [person1, person2];
petArray = [pet1, pet2];
In the example I would like to combine the personArray and petArray into a new array using the age property of each to sort them into the array by order of youngest to oldest. Any help is appreciated.