Sort Of an Array
Object: Array_name
Method or Function: sort()
Syntax: Array_name.sort()
The sort method sorts array elements either alphabetic or numeric, and either ascending or descending.
Sort method changes the original array.
When nothing is passed to the sort method, every value is converted to a string and sorted in lexicographic order.
We have twelve months, January through December in array month.
We will sort these twelve months alphabetically.
Example
<script type="text/javascript">
var month = new Array("January" , "February", "March", "April", "May", "June","July", "August",
"September", "October", "November", "December");
var string = month.sort();
document.write(string);
</script>
April,August,December,February,January,July,June,March,May,
November,October,September
No comments:
Post a Comment