From: David ‘Bombe’ Roden Date: Thu, 19 Apr 2012 11:58:55 +0000 (+0200) Subject: Add sort filter to template context factory. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=5fddefcaa9c8d2531210910ab34636dd25611c31;p=demoscenemusic.git Add sort filter to template context factory. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java index 917fad7..9cfd7f5 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java @@ -26,6 +26,7 @@ import java.io.Reader; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.net.URISyntaxException; +import java.util.Comparator; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -36,9 +37,11 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import net.pterodactylus.demoscenemusic.data.Artist; import net.pterodactylus.demoscenemusic.page.ServletRequest; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.io.StreamCopier; +import net.pterodactylus.util.template.CollectionSortFilter; import net.pterodactylus.util.template.HtmlFilter; import net.pterodactylus.util.template.ReflectionAccessor; import net.pterodactylus.util.template.Template; @@ -75,6 +78,16 @@ public class TemplateServlet extends HttpServlet { templateContextFactory.addAccessor(Object.class, new ReflectionAccessor()); templateContextFactory.addFilter("html", new HtmlFilter()); + CollectionSortFilter sortFilter = new CollectionSortFilter(); + sortFilter.addComparator(Artist.class, new Comparator() { + + @Override + public int compare(Artist leftArtist, Artist rightArtist) { + return leftArtist.name().compareToIgnoreCase(rightArtist.name()); + } + + }); + templateContextFactory.addFilter("sort", sortFilter); templateContextFactory.addTemplateObject("core", core); templateContextFactory.addTemplateObject("dataManager", core.getDataManager());