Sort Date Array in Javascript

Results 1 to 2 of 2

Thread: Sort Date Array in Javascript

  1. #1
    Join Date
    Dec 1969

    Default Sort Date Array in Javascript

    Hi All,<BR><BR>How can I sort an array in javascript in which all elements are dates.<BR><BR> a = new Array("1/2/2000", "12/31/1999", "1/1/2000") If i use the array.sort function than it converts all dates into string and try to sort and result is not right. Any help please,<BR><BR><BR>Thanks in advance,<BR>Anil

  2. #2
    Join Date
    Dec 1969

    Default RE: Sort Date Array in Javascript

    Change how you create the array:<BR><BR>Instead of<BR><BR>a = new Array("1/2/2000", "12/31/1999", "1/1/2000") <BR><BR>do<BR><BR>a = new Array( new Date(2000,1,2), new Date(1999,12,31), newDate(2000,1,1) );<BR><BR>and then a.sort should work correctly because the dates are now true data objects.<BR><BR>The JS specs say you could also do:<BR><BR>a = new Array( new Date("January 2, 2000"), new Date("December 31, 1999"), newDate("January 1, 2000") );<BR><BR>but that doesn&#039t strike me as any easier to create than the numeric form. But your choice.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts