Code Lab

Sort by age

EasyFunctions5 min

Write a function sortByAge(users). It gets an array of users like { name, age } and sorts it by age, from the youngest to the oldest.

Sort the array itself and return it.

Examples

sortByAge([{ name: 'Pete', age: 30 }, { name: 'John', age: 25 }, { name: 'Mary', age: 28 }])

Returns [ { name: 'John', age: 25 }, { name: 'Mary', age: 28 }, { name: 'Pete', age: 30 } ]

Source

Loading the editor...
  1. InputsortByAge([ { name: 'Pete', age: 30 }, { name: 'John', age: 25 }, { name: 'Mary', age: 28 } ])
    Expected[ { name: 'John', age: 25 }, { name: 'Mary', age: 28 }, { name: 'Pete', age: 30 } ]
  2. Sorts the array itself and returns it

Hidden tests run on Submit.