Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Sort an Array of Objects</title> </head> <body> <script> let persons = [ { name: "Harry", age: 14 }, { name: "Ethan", age: 30 }, { name: "Peter", age: 21 }, { name: "Clark", age: 42 }, { name: "Alice", age: 16 } ]; // Sort by age persons.sort(function (a, b) { return a.age - b.age; }); console.log(persons); // Sort by name persons.sort(function(a, b) { let x = a.name.toLowerCase(); // ignore upper and lowercase let y = b.name.toLowerCase(); // ignore upper and lowercase if(x < y) { return -1; } if(x > y) { return 1; } // names must be equal return 0; }); // Loop through all the elements in the array for(let i in persons) { // Loop through all the properties in the object for(let prop in persons[i]) { document.write(prop + ": " + persons[i][prop] + "<br>"); } document.write("<hr>"); } </script> </body> </html>