JavaScript | typedArray.sort() with Examples

The typedArray.sort() is an inbuilt function in JavaScript which is used to sort the elements of the given typedArray and return the new typedArray with the sorted elements.
Syntax:

typedArray.sort();

Parameter: It does not accept any parameters.
Return value: It returns the new typedArray with the sorted elements.
JavaScript code to show the working of above function:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Creating some typedArrays with some elements
const A = new Uint8Array([ 5, 9, 1, 0, 45, 2 ]);
const B = new Uint8Array([ 22, 9, 1, 20 ]);
const C = new Uint8Array([ 5, 9, 0, 3, 1 ]);
const D = new Uint8Array([ 1, 3, 100, 42, 4 ]);
  
// Calling sort() function on the above typedArray
a = A.sort();
b = B.sort();
c = C.sort();
d = D.sort();
  
// Printing the sorted typedArray
document.write(a + "<br>");
document.write(b + "<br>");
document.write(c + "<br>");
document.write(d + "<br>");
  
</script>

chevron_right


Output:

0, 1, 2, 5, 9, 45
1, 9, 20, 22
0, 1, 3, 5, 9
1, 3, 4, 42, 100


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.