JavaScript TypedArray.prototype.toSorted() Method Last Updated : 29 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The toSorted() method, presented in ECMAScript 2023 or (ES2023) affords a secure and efficient manner of ordering elements of TypedArray in ascending order. The toSorted() method of TypedArray instances is the copying version of the sort() method, it gives back a new typed array with sorted elements. Its algorithm is the same as that of Array.prototype.toSorted one apart from it uses numeric sorting rather than string sorting by default. SyntaxtoSorted()toSorted(compareFn)Parameter:compareFn: A function that determines the order of the elements, if omitted, the typed array elements are sorted according to a numeric value. Return Type:Returns a new TypedArray that has been created with elements in ascending order. Example 1: In this example, we are sorting numbers. JavaScript const numbers = new Int16Array([5, 1, 8, 3, 2]); const sortedNumbers = numbers.toSorted(); console.log(sortedNumbers); console.log(numbers); Output: Int16Array(5) [1, 2, 3, 5, 8]Int16Array(5) [5, 1, 8, 3, 2]Example 2: In this example, we are sorting strings. JavaScript const fruits = new Uint8Array([79, 97, 100, 101, 82, 99]); function stringCompare(a, b) { return String.fromCharCode(a).localeCompare(String.fromCharCode(b)); } const sortedFruits = fruits.toSorted(stringCompare); console.log(String.fromCharCode.apply(null, sortedFruits)); Output: acdeORSupported BrowsersChromeEdgeFirefoxOperaSafari Comment More infoAdvertise with us Next Article JavaScript TypedArray.prototype.toReversed() Method P pankajbind Follow Improve Article Tags : JavaScript Web Technologies javaScript Similar Reads JavaScript TypedArray.prototype.toReversed() Method In JavaScript, Array.prototype.toReversed() method is used to reverse an array. It returns a new array that has elements in reverse order. Syntax :toReversed()Parameter:This method does not accept any parameter. Return Value:This method returns a newly formed reverse array. Example: This example sho 1 min read 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 1 min read JavaScript typedArray.reverse() Method The typedArray.reverse() is an inbuilt function in JavaScript which is used to reverse the given typedArray elements i.e, first element becomes the last and vice versa. Syntax: typedArray.reverse(); Parameter: It does not accept any parameters. Return value: It returns the new reversed typedArray. E 1 min read JavaScript typedArray.of() Method The typedArray.of() is an inbuilt function in JavaScript which is used to construct a new typedArray with a variable number of parameters. Syntax: TypedArray.of(element0, element1, ......) Parameters: It accepts parameters of different elements whose typedArray is going to be created. Return value: 1 min read JavaScript typedArray.from() Property The typedArray.from() is an inbuilt function in JavaScript which is used to construct a new typedArray from a normal array or any iterable object. List of different typedArrays are specified in the table below: Int8Array();Int16Array();Uint32Array();Uint8Array();Uint16Array();Float32Array();Uint8Cla 2 min read JavaScript typedArray.lastIndexOf() Method The typedArray.lastIndexOf() is an inbuilt function in JavaScript which is used to return the last index of the typedArray at which a given element is present otherwise it returns -1 if the element is not present. Syntax: typedArray.lastIndexOf(search_Element, from_Index]) Parameters: It accepts two 2 min read Like