/    Sign up×
Community /Pin to ProfileBookmark

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’];
[/code]

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

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledApr 10.2009 — start looking into the [b][url=http://www.w3schools.com/jsref/jsref_sort.asp]sort[/url][/b] 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!
Copy linkTweet thisAlerts:
@JMRKERApr 10.2009 — Hello All,

I've an array of month/year to sort

<i>
</i>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[/QUOTE]


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.
Copy linkTweet thisAlerts:
@dingbatApr 10.2009 — 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));[/CODE]
Copy linkTweet thisAlerts:
@astupidnameApr 10.2009 — You could just turn each date in the array into an actual date within the custom sort function and sort it by actual date:

[code=html]<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>[/code]

Of course, it would be a bit simpler within the code and more versatile if you used a four place year format.
Copy linkTweet thisAlerts:
@astupidnameApr 10.2009 — 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:
[code=html]<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>[/code]
Copy linkTweet thisAlerts:
@KalyanRajauthorApr 14.2009 — ?

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

Success!

Help @KalyanRaj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...