From 5fddefcaa9c8d2531210910ab34636dd25611c31 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 19 Apr 2012 13:58:55 +0200 Subject: [PATCH] Add sort filter to template context factory. --- .../pterodactylus/demoscenemusic/core/TemplateServlet.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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()); -- 2.7.4