Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Create a function to sort an array of objects by a given property.

Medium

Problem Statement

Create a function to sort an array of objects by a given property.

Sample Input

[ { name: "John", age: 30 }, { name: "Alice", age: 25 }, { name: "Bob", age: 35 } ]

Sample Output

[ { name: 'Alice', age: 25 },
  { name: 'John', age: 30 },
  { name: 'Bob', age: 35 } ]

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
// Sort an array of objects by a specified property function sortByProperty(arr, property) { } let data = [ { name: "John", age: 30 }, { name: "Alice", age: 25 }, { name: "Bob", age: 35 } ]; console.log(sortByProperty(data, "age"));
Log in or sign up to run or submit your code.Log inSign up
Input
[ { name: "John", age: 30 }, { name: "Alice", age: 25 }, { name: "Bob", age: 35 } ]
Expected Output
[ { name: 'Alice', age: 25 },
  { name: 'John', age: 30 },
  { name: 'Bob', age: 35 } ]