If I'm understanding the problem correctly, you might be allowed to do a simple bubble sort.
Assuming you had something like this
var students = [{
name: 'Mark',
age: 28
},
{
name: 'Johnson',
age: 30
},
{
name: 'Williams',
age: 12
},
{
name: 'Henry',
age: 27
}
]
console.log(bubbleSort(students.map(x => x.age)))
You might be able to use one of these two bubble sort algorithms
Using a foreach and a for loop
let bubbleSort = (inputArr) => {
let len = inputArr.length;
inputArr.forEach(function(i){
for (let j = 0; j < len; j++) {
if (inputArr[j] > inputArr[j + 1]) {
let tmp = inputArr[j];
inputArr[j] = inputArr[j + 1];
inputArr[j + 1] = tmp;
}
}
})
return inputArr;
};
Using a foreach and a do/while loop
let bubbleSort = (inputArr) => {
let len = inputArr.length;
let swapped;
do {
swapped = false;
inputArr.forEach(function(number, i){
if (inputArr[i] > inputArr[i + 1]) {
let tmp = inputArr[i];
inputArr[i] = inputArr[i + 1];
inputArr[i + 1] = tmp;
swapped = true;
}
})
} while (swapped);
return inputArr;
};
https://www.studytonight.com/data-structures/bubble-sort