Last active
August 29, 2015 14:22
-
-
Save tonyjunkes/3c2e508e8b5fcd05a66c to your computer and use it in GitHub Desktop.
Blog Post: Keeping Order With Java's LinkedHashMap (With 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)} | |
]; | |
LinkedHashMap = createObject("java", "java.util.LinkedHashMap"); | |
collection = LinkedHashMap.init(); | |
for (post in articles) { | |
year = year(post.publishDate); | |
if (!structKeyExists(collection, year)) { | |
collection[year] = LinkedHashMap.init(); | |
} | |
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