Để thực hiện sắp xếp trong JavaScript, hãy sử dụng hàm sort (). Bạn có thể thử chạy các giá trị số sắp xếp mã sau.
Ví dụ
<!DOCTYPE html>
<html>
<body>
<script>
var arr = [75, 20, 90, 9, 49, 32, 69, 66];
document.write("Initial Values: "+arr);
function myFunction() {
arr.sort(function(val1, val2){return val1 - val2});
document.write("<br>Sorted Values: "+arr);
}
myFunction();
</script>
</body>
</html>