Last active
August 29, 2015 14:22
-
-
Save tonyjunkes/6d678f1a7800423a9484 to your computer and use it in GitHub Desktop.
Blog Post: Keeping Order With Java's LinkedHashMap (No LinkedHashMap Example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// Some dummy articles yay! | |
articles = [ | |
{title: "I Am a Title Perhaps", publishDate: createDate(2015, 11, 08)}, | |
{title: "I Am a Title As Well", publishDate: createDate(2014, 08, 15)}, | |
{title: "I Am a Title I Think", publishDate: createDate(2014, 09, 25)}, | |
{title: "I Am a Title", publishDate: createDate(2013, 04, 05)}, | |
{title: "I Am a Title Too", publishDate: createDate(2013, 05, 17)}, | |
{title: "I Am a Title Also", publishDate: createDate(2013, 07, 02)} | |
]; | |
collection = {}; | |
for (post in articles) { | |
year = year(post.publishDate); | |
if (!structKeyExists(collection, year)) { | |
collection[year] = {}; | |
} | |
if (!structKeyExists(collection[year], monthAsString(month(post.publishDate)))) { | |
collection[year][monthAsString(month(post.publishDate))] = []; | |
} | |
arrayAppend( | |
collection[year][monthAsString(month(post.publishDate))], | |
{"title": post.title, "date": post.publishDate} | |
); | |
} | |
writeDump(collection); | |
</cfscript> | |
<cfoutput> | |
<cfloop item="year" collection="#collection#"> | |
<h4>#year#</h4> | |
<cfloop item="month" collection="#collection[year]#"> | |
<div><strong>#month#</strong></div> | |
<cfloop index="article" array="#collection[year][month]#"> | |
<div>#article.title# | #dateFormat(article.date, "YYYY/MM/DD")#</div> | |
</cfloop> | |
</cfloop> | |
</cfloop> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment