เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับอาร์เรย์ของ Numbers
จากนั้นฟังก์ชันควรจัดเรียงอาร์เรย์ของตัวเลขให้เข้าที่ (เรียงลำดับจากน้อยไปมากหรือมากไปหาน้อย)
ตัวอย่าง
รหัสสำหรับสิ่งนี้จะเป็น −
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 ]