Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ฟังก์ชัน TypedArray.sort() ใน JavaScript

TypedArray.sort()

เมธอด sort() เรียงลำดับสมาชิกใน Typed Array จากน้อยไปมาก (Ascending) และส่งคืน Array เดียวกันที่ถูกเรียงแล้ว (In-place)

ไวยากรณ์

typedArray.sort()

ตัวอย่าง

<html> <body> <script>
  var typedArray = new Int32Array([11, 5, 13, 4, 15, 3, 17, 2, 19, 8]);
  document.write("Contents of the typed array: " + typedArray);
  document.write("<br>");
  var resultantArray = typedArray.sort();
  document.write("Resultant Array: " + resultantArray);
</script> </body> </html>

ผลลัพธ์

Contents of the typed array: 11,5,13,4,15,3,17,2,19,8
Resultant Array: 2,3,4,5,8,11,13,15,17,19

หมายเหตุ: ต่างจาก Array.sort() ทั่วไปที่แปลงค่าเป็น String ก่อนเรียง TypedArray.sort() เรียงแบบตัวเลขจริง จึงไม่ต้องส่งฟังก์ชัน Compare Function