var persons = [ {name: "Иван", age: 17}, {name: "Мария", age: 35}, {name: "Алексей", age: 73}, {name: "Яков", age: 12}, ] function sort(arr, key, boolean = true) { let middleArr = []; for (let i = 0; i < arr.length; i++) { middleArr.push(arr[i][key]); } for (let j = 0; j < middleArr.length; j++) { let index = 0 for (let k = 1; k < middleArr.length;) { if (middleArr[index] > middleArr[k]) { [middleArr[index], middleArr[k]] = [middleArr[k], middleArr[index]] k += 2 index += 1 } else { k += 1 index += 1 } } } if (boolean == false) { middleArr.reverse() } return middleArr } console.log(sort(persons, "age")); console.log(sort(persons, "name", false))