Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Sort an Array of Objects by Numeric Property Values</title> <script> // Defining comparison function function compareAges(a, b) { return a.age - b.age; } // Sample object array var objArr = [ { name: "Harry", age: 14 }, { name: "Peter", age: 20 }, { name: "Alice", age: 18 } ]; // Perform sorting objArr.sort(compareAges); document.write(JSON.stringify(objArr, null, 4)); </script> </head> <body style="font-family: monospace; white-space: pre;"> <!-- Result will be printed here --> </body> </html>