เราต้องเขียนฟังก์ชัน JavaScript ที่รับอาร์เรย์ของตัวเลขเข้ามา ฟังก์ชันควรจัดเรียงอาร์เรย์ของตัวเลขในที่เดิม (in-place) ไม่ว่าจะเป็นลำดับเพิ่มขึ้นหรือลดลง
ตัวอย่างโค้ด
โค้ดสำหรับทำสิ่งนี้คือ:
const arr = [2, 5, 19, 2, 43, 32, 2, 34, 67, 88, 4, 7];
const sortIntegers = (arr = []) => {
const sorterAscending = (a, b) => {
return a - b;
};
const sorterDescending = (a, b) => {
return b - a;
};
arr.sort(sorterAscending);
};
sortIntegers(arr);
console.log(arr);
ผลลัพธ์
ผลลัพธ์ในคอนโซลจะเป็นดังนี้:
[ 2, 2, 2, 4, 5, 7, 19, 32, 34, 43, 67, 88 ]