-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_sort.js
More file actions
42 lines (32 loc) · 1.27 KB
/
array_sort.js
File metadata and controls
42 lines (32 loc) · 1.27 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
33
34
35
36
37
38
39
40
41
42
// How to sort an array of objects?
const objects = [
{ first: 'John', last: 'Jo', age: 10 },
{ first: 'Mary', last: 'Maya', age: 40 },
{ first: 'Chris', last: 'Cran', age: 33 },
{ first: 'Bob', last: 'Brend', age: 22 },
{ first: 'Tim', last: 'Tyler', age: 12 },
{ first: 'David', last: 'Duncan', age: 55 }
]
// Solution 1: Using slice() to avoid mutation
// Make the sorting case-insensitive
const sorted = objects.slice().sort((a, b) => {
if (a.last.toLowerCase() < b.last.toLowerCase()) {
return -1;
} else {
return 1;
}
})
console.log({ objects }); // Original array remains unchanged
console.log({ sorted }); // Sorted array
// Solution 2: Inline function
const sorted2 = objects.sort((a, b) => a.last < b.last ? 1 : -1);
console.log({ objects }); // Original array is now sorted
console.log({ sorted2 });
// Solution 3: Using localeCompare() for string comparison
const sorted3 = objects.sort((a, b) => a.last.localeCompare(b.last));
console.log({ objects }); // Original array is now sorted
console.log({ sorted3 });
// Solution 4: Sorting by a different property (e.g., age)
const sorted4 = objects.sort((a, b) => a.age < b.age ? 1 : -1);
console.log({ objects }); // Original array is now sorted by age
console.log({ sorted4 });