Web | This Site

JavaScript Basics
JavaScript Objects
JavaScript Advanced
JS Useful References
JS Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# Javascript Array sort() Method

## 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.

Advertisement

Online Image Processing

Indian Baby Names