Wednesday, July 3, 2024
HomeLanguagesJavascriptJavaScript typedArray.sort() Method

JavaScript typedArray.sort() Method

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. 

Example:

javascript




// 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
console.log(a);
console.log(b);
console.log(c);
console.log(d);


Output:

0, 1, 2, 5, 9, 45
1, 9, 20, 22
0, 1, 3, 5, 9
1, 3, 4, 42, 100
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments