1. Registered User
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. 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!

3. Originally Posted by KalyanRaj
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. Registered User
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'];
Last edited by dingbat; 04-10-2009 at 01:44 PM.

5. Registered User
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],
bd = new Date(bs[0]+' 1,'+bsyr);
}

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. Registered User
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('/'),
bd = new Date(bs[0]+' 1,'+bs[1]);
}

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. Registered User
Join Date
May 2007
Posts
8

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

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
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.41784 seconds
• Memory Usage 2,908KB
• Queries Executed 15 (?)
Template Usage (35):
• (3)bbcode_code
• (2)bbcode_html
• (1)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (7)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (7)postbit
• (7)postbit_onlinestatus
• (7)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete