Sorting an array in alphabetical order
The general syntax of the sort() method is as
follows:
The sort() method by default
sorts the values inside an array in alphabetical order (dictionary order):
var myarray=new Array("Bob","Bully","Amy")
myarray.sort()
//myarray[0] is now Amy, myarray[1] is now Bob, myarray[2] is now Bully
Before you starting wondering
off, consider what happens if we sort numbers instead:
var myarray=new Array(7,40)
myarray.sort()
//myarray[0] is now 40, myarray[1] is now 7
Although 7 is numerically
smaller than 40, alphabetically , it is larger, since 7 itself is larger
than 4. The sort() method by default
always treats values as strings and sorts them as such. If you're confused
about the criteria the sort() method uses when
sorting, simply flip through a dictionary. The way the words in a dictionary
are indexed follows the same rule.
|