Previous: DeadJournal Dept.

Library Alpha Sort In Movable Type

For a while I've been trying to figure out how to get the Movies and Books sections of this site to have alpha-sorted listings with library-style alphabetization — in other words, so that titles starting with "A/An/The" don't get sorted on those words. I thought about creating a plugin to do this, since there's no native sort order of that variety in Movable Type, but instead I went with a template solution. It's not all that slow, and since I have these alpha indexes built in the background, it doesn't matter.

I created a template with two loop blocks:

<mt:Entries><mt:setvarblock name="mytitle">''<mt:EntryBasename regex="s/^(a|an|the)_(.*)/$2/gi" />''|<mt:EntryID></mt:setvarblock><mt:setvar function="push" name="myarray"  value="$mytitle"/></mt:Entries>

<mt:loop  name="myarray" sort_by="value">
<mt:setvarblock  name="itemid"><mt:var  name="__value__" regex="s/''(.*)?''\|(.*)/$2/gi"></mt:setvarblock>
<mt:entries  id="$itemid" limit="1">
<$mtinclude  module="Entry Listing"$>

The first loop block (the mt:Entries block) creates a list of all the available entries for the category in question, transforms them with a regex so that the name has "A/An/The" stripped out, combines the name with its entry ID in a consistent way that's easy for a regex to re-extract, and builds an array out of them. The elements in the array look like this:

''28 Days Later''|2187
''A Time To Live And A Time To Die''|2402


The second loop takes the array, sorts it, extracts the ID number for the entry, and uses that to generate all the relevant information about the entry (its real name, for instance). The Entry Listing module is an include I used to display the entry name and some additional data (e.g., whether or not there's video on the web available for it).

I was originally going to use a hashtable instead of an array, but this turned out to be easier for me to wrap my head around at this stage of the game.

NOTE: In the above code examples I've inserted linebreaks for readability. I strongly recommend stripping those out before using this in a production environment.

Tags: Movable Type  programming 

comments powered by Disqus

Previous: DeadJournal Dept.

About This Page

This page contains a single entry by Serdar Yegulalp in the category Uncategorized / General, published on 2009/09/29 12:39.

See all entries for September 2009.

See all entries in 2009.

Find recent content on the main index or look in the archives to find all content.

My Books

Out Now

Previously Released

More about my books

Search This Site