Tutorials Point


  JavaScript Basics
  JavaScript Objects
  JavaScript Advanced
  JS Useful References
  JS Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Javascript Array sort() Method


previous next AddThis Social Bookmark Button


Advertisements

Description:

Javascript array sort() method sorts the elements of an array.

Syntax:

array.sort( compareFunction );

Here is the detail of parameters:

  • compareFunction : Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically.

Return Value:

Returns a sorted array.

Example:

<html>
<head>
<title>JavaScript Array sort Method</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array("orange", "mango", "banana", "sugar");

var sorted = arr.sort();
document.write("Returned string is : " + sorted );

</script>
</body>
</html>

This will produce following result:

Returned array is : banana,mango,orange,sugar 

To understand it in better way you can Try it yourself.



previous next Printer Friendly

Advertisements


  

Advertisements