Library Alpha Sort In Movable Type

Sep 29, 2009 12:41


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:

''regex="s/^(A |An |The )(.*)/$2/gi" />''|
function="push" name="myarray" value="$mytitle"/>

regex="s/''(.*)?''\|(.*)/$2/gi">

<$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
(etc.)

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.

[Comment here]

programming, movable type

Previous post Next post
Up