1var array = [
2 {name: "John", age: 34},
3 {name: "Peter", age: 54},
4 {name: "Jake", age: 25}
5];
6
7array.sort(function(a, b) {
8 return a.age - b.age;
9}); // Sort youngest first
1// Sort an array of numbers
2let numbers = [5, 13, 1, 44, 32, 15, 500]
3
4// Lowest to highest
5let lowestToHighest = numbers.sort((a, b) => a - b);
6//Output: [1,5,13,15,32,44,500]
7
8//Highest to lowest
9let highestToLowest = numbers.sort((a, b) => b-a);
10//Output: [500,44,32,15,13,5,1]
11
1 strData = ["banana", "apple", "zebra", "jos"];
2 numData = [1, 3, 2, 5, 6];
3 objData = [
4 { name: 'Alex', age: 20 },
5 { name: 'Karlsson', age: 19 },
6 { name: 'Noah', age: 21 },
7 ];
8
9
10 // sort str alfabeticly (a-z)
11 strData.sort((a,b) => {
12 return a === b ? 0 : a < b ? -1 : 1
13 });
14 console.log(strData)
15
16
17 // sort Objdata uppifrån å ner (ålder 19-21)
18 objData.sort((a,b) => a.age - b.age)
19 console.log(objData);
20
21
22 // Small number to large number
23 numData.sort()
24 console.log(numData);
25 // Large number to small number
26 numData.sort((a,b) => b-a)
27 console.log(numData);
1let numbers = [4, 10, 5, 1, 3];
2numbers.sort((a, b) => a - b);
3console.log(numbers);
4
5// [1, 2, 3, 4, 5]