CodesJava

Easy learning with example program codes

JavaScript Array sort() method

The JavaScript array sort() method is used to return the element of the given array in a sorted order.

Syntax:

array.sort(compareFunction)

Parameters:
compareFunction: It represents a function which provides the functionality of sort order. It is optional parameter.

Returns:
An array with sorted elements.

Example:

<!DOCTYPE html>
<html>
<body>
<script>  
var a = ["GOLD","SILVER","DIAMOND","RUBY","PLATINUM"]
var result=a.sort();  
document.writeln(result);  
</script>  
</body>
</html>

Output:

DIAMOND,GOLD,PLATINUM,RUBY,SILVER
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status