Answer the question
In order to leave comments, you need to log in
How to write code correctly without mutating sort?
Hello, friends! please tell me how to rewrite the code that contained the mutating sort method to the code without it (maybe using map)?
const age = person.sort((item1, item2) => (item1.age - item2.age));
Answer the question
In order to leave comments, you need to log in
const age = person => {
let amount;
for (let i = 0; i < person.length; i++) {
for (let j = i; j < person.length; j++) {
if (person[i] > person[j]) {
amount = person[i];
person[i] = person[j];
person[j] = amount;
}
}
}
return person;
}
const res = [3, 4, 2, 78, 0, 1];
console.log(age(res));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question