Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Blog Post: Keeping Order With Java's LinkedHashMap (With LinkedHashMap Example)
<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
You can’t perform that action at this time.