JavaScript TypedArray.prototype.toSorted() Method Last Updated : 29 May, 2024 Comments Improve Suggest changes 4 Likes 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 Create Quiz Comment P pankajbind Follow 4 Improve P pankajbind Follow 4 Improve Article Tags : JavaScript Web Technologies javaScript Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like