Array - sort() : Array « Language Basics « JavaScript DHTML

Array - sort()


<script type="text/javascript">
array1=new Array("Red","Blue","Green")
array2=new Array("73","19","830")
array3=new Array(510,130,23,3400)
array4=new Array("72","83","850",3,1,5,40)

function compareNum(a,b){
    return a-b

document.write("Sorted: " + array1.sort())

document.write("Sorted without compareNum: " + array2.sort())
document.write("Sorted with compareNum: " + array2.sort(compareNum))

document.write("Sorted without compareNum: " + array3.sort())
document.write("Sorted with compareNum: " + array3.sort(compareNum))

document.write("Sorted without compareNum: " + array4.sort())
document.write("Sorted with compareNum: " + array4.sort(compareNum))



Related examples in the same category

1.Demo all methods in Array
2.Assing array value inside function
3.Simple Array Demo
4.Array loop, find:Control array : Two dimension array
5.Reversing, Sorting, and Concatenating an Array
6.Custom Numeric Comparison for the Array.Sort Method
7.Case-Insensitive Comparison for the Array.Sort Method
8.Iterating Through a Sparse Array
9.Using Functions to Iterate Through an Array
10.Reading and Writing Array Elements
11.Array with a numeric parameter and assign data to it
12.A string array
13.Array - properties and methods:length, join, reverse, push,pop,shift
14.Array - concat and slice
15.Array - splice
16.Methods and Properties of the Array Object
17.Displaying the Contents of an Array
18.Using the Array.join() Method
19.Using JavaScript Arrays
20.Extending the Length of an Array
21.An Array within an Array
22.Using the Methods of the Array object
23.Array.sort() Possibilities
24.Array.reverse() Method
25. Array Concatenation
26.A Looping Array Lookup
27.A Simple Parallel Array Lookup
28.Adding a prototype Property
29.Two-Dimensional Array Work Around
30.Array definition and iteration
31.Reference an Array by index
32.URL Array
33.Array Utility functions
34.Dynamic array
35.Use for loop to display elements in an array
36.A string array variable
37.Queue based on array