Write a function sortBy(arr, key, order) that returns a new array of objects sorted by the given key in either 'asc' (ascending, default) or 'desc' (descending) order without mutating the original array. For example, sortBy([{age: 30}, {age: 20}], 'age', 'desc') should return [{age: 30}, {age: 20}].
function sortBy(arr, key, order = 'asc') {
// Your code here
}
Rules:
Do not mutate the input array.
Handle numeric and string property values.
Default to ascending order when the order parameter is omitted.
Post your solution as a reply. Answer goes up in about a day.