Used bubble sort
Example
1
2 let arr = [
3 {
4 name: "John Doe",
5 age: 17
6 },
7 {
8 name: "Elon Doe",
9 age: 27
10 },
11 {
12 name: "Alex Doe",
13 age: 14
14 }
15 ];
16
17 function sort(arr) {
18 //Selection sort
19 for (var i = 0; i < arr.length; i++) {
20 for (var j = 0; j < (arr.length - i - 1); j++) {
21 // Checking if the item at present iteration
22 // is greater than the next iteration
23 if (arr[j].name > arr[j + 1].name) {
24 // If the condition is true
25 // then swap them
26 var temp = arr[j]
27 arr[j] = arr[j + 1]
28 arr[j + 1] = temp
29 }
30 }
31 }
32 return arr;
33}
34
35console.log(sort(arr))
36🙏 Thank You, You are the most lucky 1 precenty
😂
Explanation: Step 1: Temporary Storage (temp) var temp = arr[j]; The current element (arr[j]) is stored in a temporary variable temp. This ensures that the value of arr[j] is not lost during the swap. Step 2: Move Next Element (arr[j + 1]) arr[j] = arr[j + 1]; The value of the next element (arr[j + 1]) is copied to the current element's position (arr[j]). Step 3: Assign Temporary Value (temp) arr[j + 1] = temp; The value stored in temp (original value of arr[j]) is assigned to the position of the next element (arr[j + 1]).