-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path9.SortingObjectsByProperty.js
More file actions
32 lines (25 loc) · 1.02 KB
/
9.SortingObjectsByProperty.js
File metadata and controls
32 lines (25 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 9. Sorting Objects by Property
// Problem: Write a function to sort an array of objects by a specific property.
function sortByProperty(arr, property) {
return arr.slice().sort((a, b) => {
if (a[property] < b[property]) return -1;
if (a[property] > b[property]) return 1;
return 0;
});
}
// Test cases
const input = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 22 },
{ name: 'Charlie', age: 30 }
];
console.log(sortByProperty(input, 'age'));
// Output: [{ name: 'Bob', age: 22 }, { name: 'Alice', age: 25 }, { name: 'Charlie', age: 30 }]
console.log(sortByProperty(input, 'name'));
// Output: [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 22 }, { name: 'Charlie', age: 30 }]
// Explanation:
// -The slice() method creates a shallow copy of the array to prevent mutation.
// -The sort() method is used with a custom comparator function:
// oReturn -1 if a[property] < b[property], meaning a should come before b.
// oReturn 1 if a[property] > b[property].
// oReturn 0 if they are equal.