Chúng ta cần viết hàm JavaScript nhận vào mảng số. Hàm sẽ sắp xếp mảng tại chỗ (tăng dần hoặc giảm dần).
Ví dụ
Mã thực hiện như sau:
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);
Kết quả
Kết quả trong console sẽ là:
[
2, 2, 2, 4, 5,
7, 19, 32, 34, 43,
67, 88
]