I need to sort an array that looks like this:
var array = new Array();<br />
array[0]="201206031245 firstitem";<br />
array[1]="201206020800 seconditem";<br />
array[2]="201206040604 itemthree";<br />
array[3]="201206031345 lastitem";<br />
How would I sort this numerically and descending?
Thanks in advance!
Although
.sort()will by default do an alphanumeric sort, for your data it will work numerically because your array elements all start with numbers that follow a strict date/time format with the same number of digits..sort()will sort ascending though. You could provide your own comparison function to sort descending, or you could just reverse the results:For more information about how
.sort()works, e.g., to provide your own comparison function, have a look at the documentation.