TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript TypedArray sort() Method

JavaScript TypedArray sort() Method with example, join() method, javascript typedarray methods, keys() method, indexOf() method, includes() method, forEach() method, entries(), every(), find(), filter(), copyWithin() method etc.

<< Back to JAVASCRIPT

JavaScript TypedArray sort() Method

The JavaScript sort() method is used to sort the array and returns the updated array. The array can be any type like- string, numbers, and character.

Syntax:

Array.sort()

Parameters:

No parameters.

Return value:

The sorted array.

Browser Support:

Chrome Yes
Safari Yes
Firefox Yes
Opera Yes

Example 1

JavaScript TypedArray sort() Method.

<script>
// JavaScript to illustrate sort() method
function JavaTpoint() {
   //Original string
    var arr = [2, 5, 8, 1, 4]
 //Sorting the array
    document.write(arr.sort());
    document.write("<br>");
    document.write(arr);
// expected output: arr[Output:1,2,4,5,8]
}
JavaTpoint();
</script>
Test it Now

Output:

1,2,4,5,8

Example 2

JavaScript TypedArray sort() Method.

<script>
// JavaScript to illustrate sort() method
 function JavaTpoint() {
 // Original array
    var arr = [2, 5, 8, 1, 4];
 document.write(arr.sort(function(a, b) {
    return a=b;
}));
document.write("<br>");
document.write(arr);
// expected output: arr[Output:4,1,8,5,2]
}
JavaTpoint();
</script>
Test it Now

Output:

4,1,8,5,2

Example 3

JavaScript TypedArray sort() Method.

<script>
// JavaScript to illustrate sort() method
 function JavaTpoint() {
   // Original array
    var arr = [2, 5, 8, 1, 4];
    document.write(arr.sort(function(a, b) {
    return a<b;
}));
document.write("<br>");
document.write(arr);
// expected output: arr[Output:8,5,4,2,1]
}
JavaTpoint();
</script>
Test it Now

Output:

8,5,4,2,1

Example 4

JavaScript TypedArray sort() Method.

<script>
// JavaScript to illustrate sort() method
 function JavaTpoint() {
 // Original array
 var arr = [2, 5, 8, 1, 4];
 document.write(arr.sort(function(a, b) {
    return a>b;
}));subarray 
document.write("<br>");
document.write(arr);
// expected output: arr[Output:1,2,4,5,8]
}
JavaTpoint();
</script>
Test it Now

Output:

1,2,4,5,8





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf