www.webdeveloper.com
Results 1 to 7 of 7

Thread: Sort Month/Year array

  1. #1
    Join Date
    May 2007
    Posts
    8

    Sort Month/Year array

    Hello All,

    I've an array of month/year to sort

    Code:
    var myArray = ['Oct/08', 'Jan/09', 'Mar/09', 'May/07', 'Apr/08', 'Dec/06'];
    I've an idea that declare a hash of month with numbers as keys, split each array element and compare each month and sort it. Please help me out as I don't know how to start with

    Regards,
    Kalyan Raj

  2. #2
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    start looking into the sort method, specially the arguments it accept. it accept a function as a parameter and that function is used to determine how to sort each element. basically you pass a function that do the following. create the hash for months. split each arguments passed to your function. check if the year are the same. if they aren't, subtract each year and return the value. if they are the same, use the month with the hash so that each value paired with the hash are subtracted. then return that subtraction. try it, it work!
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Exclamation

    Quote Originally Posted by KalyanRaj View Post
    Hello All,

    I've an array of month/year to sort

    Code:
    var myArray = ['Oct/08', 'Jan/09', 'Mar/09', 'May/07', 'Apr/08', 'Dec/06'];
    I've an idea that declare a hash of month with numbers as keys, split each array element and compare each month and sort it. Please help me out as I don't know how to start with

    Regards,
    Kalyan Raj
    var myArray = ['Oct/08', 'Jan/09', 'Mar/09', 'May/07', 'Apr/08', 'Dec/06'].sort();

    will re-arrange your array, but I don't think it will give you what you want!
    It will cause 'Apr/08' to appear before "Dec/06" and 'Jan/09".
    But that's what a sort should do.

  4. #4
    Join Date
    Mar 2009
    Posts
    36
    Zero's right, use a custom sort function, something like this.

    Code:
    var months = new Array(12);
    months['Jan'] = 1;
    months['Feb'] = 2;
    months['Mar'] = 3;
    months['Apr'] = 4;
    months['May'] = 5;
    months['Jun'] = 6;
    months['Jul'] = 7;
    months['Aug'] = 8;
    months['Sep'] = 9;
    months['Oct'] = 10;
    months['Nov'] = 11;
    months['Dec'] = 12;
    
    function sortDate(a,b)
    {
    	var m1 = a.substring(0,3);
    	var y1 = a.substring(4);
    	var m2 = b.substring(0,3);
    	var y2 = b.substring(4);	
    
    	if(Number(y1)>Number(y2)) {
    		return 1;
    	} else if(Number(y1)<Number(y2)) {
    		return -1;
    	} else {
    		if(months[m1]>months[m2]) {
    			return 1;
    		} else if(months[m1]<months[m2]) {
    			return -1;
    		}
    	}
    
    	return 0;
    }
    
    var myArray = ['Oct/08', 'Jan/09', 'Mar/09', 'May/07', 'Apr/08', 'Dec/06'];
    alert(myArray.sort(sortDate));
    Last edited by dingbat; 04-10-2009 at 01:44 PM.

  5. #5
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    You could just turn each date in the array into an actual date within the custom sort function and sort it by actual date:

    HTML Code:
    <html>
    <body>
    <script type="text/javascript">
    
    //Will work on dates from 1910 - 2009 (limited by the two place year format in array)
    //requires date format in array to be: month/twoPlaceYear,
    //inserts a '1' between month ,year to use Date.
    //To be used within Array.sort() only with items in month/twoPlaceYear format
    function sortByMonthYearDate(a,b) {
        var as = a.split('/'),
            bs = b.split('/'),
            yr = new Date().getFullYear() - 2000,
            asyr = (as[1] > yr) ? '19'+as[1] : '20'+as[1],
            bsyr = (bs[1] > yr) ? '19'+bs[1] : '20'+bs[1],
            ad = new Date(as[0]+' 1,'+asyr),
            bd = new Date(bs[0]+' 1,'+bsyr);
        return ad.getTime() - bd.getTime();
    }
    
    var myArray = ['Oct/08', 'Apr/00', 'Jan/09', 'Mar/09', 'Aug/00', 'Jun/87', 'Feb/75', 'May/07', 'Apr/08', 'Dec/06'];
    
    document.write("Unsorted: "+myArray);
    
    myArray.sort(sortByMonthYearDate);
    document.write("<br>Sorted: "+myArray);
    /***prints:
    Feb/75,Jun/87,Apr/00,Aug/00,Dec/06,May/07,Apr/08,Oct/08,Jan/09,Mar/09 
    ***/
    
    </script>
    
    </body>
    </html>
    Of course, it would be a bit simpler within the code and more versatile if you used a four place year format.

  6. #6
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Actually, that code I posted does seem to work with four place years also (I hadn't tried until just now), but, if using four place year it could be this much simpler in the code:
    HTML Code:
    <html>
    <body>
    <script type="text/javascript">
    
    //To be used within Array.sort() on items in month/fourPlaceYear format
    //inserts a '1' between month ,year to use Date
    function sortByMonthYearDate(a,b) {
        var as = a.split('/'),
            bs = b.split('/'),
            ad = new Date(as[0]+' 1,'+as[1]),
            bd = new Date(bs[0]+' 1,'+bs[1]);
        return ad.getTime() - bd.getTime();
    }
    
    var myArray = ['Oct/2008', 'Apr/2000', 'Jan/2009', 'Jul/2020', 'May/1342', 'Mar/2009', 'Aug/2000', 'Jun/1987', 'Feb/1975', 'May/2007', 'Apr/2008', 'Dec/2006'];
    
    document.write("Unsorted: "+myArray);
    
    myArray.sort(sortByMonthYearDate);
    document.write("<br>Sorted: "+myArray);
    /***prints:
    Sorted: May/1342,Feb/1975,Jun/1987,Apr/2000,Aug/2000,Dec/2006,May/2007,Apr/2008,Oct/2008,Jan/2009,Mar/2009,Jul/2020
    ***/
    
    </script>
    
    </body>
    </html>

  7. #7
    Join Date
    May 2007
    Posts
    8


    That works perfectly for me.. Thank you all for the snippets provided.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles