Miêu tả

Phương thức array sort() trong Javascript sắp xếp phân loại các phần tử trong mảng.

Cú pháp

Cú pháp của nó như sau:

array.sort( compareFunction );

Chi tiết về tham số

compareFunction − Xác định hàm mà định nghĩa sự sắp xếp. Nếu bị bỏ qua, nó sắp xếp theo thứ tự như trong từ điển.

Trả về giá trị

Trả về mảng đã sắp xếp.

Ví dụ

Bạn thử ví dụ sau:

<html>
   <head>
      <title>JavaScript Array sort Method</title>
   </head>
   
   <body>
      <script type="text/javascript">
         var arr = new Array("orange", "mango", "banana", "sugar");
         
         var sorted = arr.sort();
         document.write("Returned string is : " + sorted );
      </script>
      
   </body>
</html>

Kết quả

Returned array is : banana,mango,orange,sugar 

Các bài học JavaScript khác tại s2sontech:




Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Learning English Everyday