C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript TypedArray sort() MethodThe 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:
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>
Output: 1,2,4,5,8 Example 2JavaScript 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>
Output: 4,1,8,5,2 Example 3JavaScript 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>
Output: 8,5,4,2,1 Example 4JavaScript 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>
Output: 1,2,4,5,8
Next TopicJavaScript TypedArray Object
|