« 
Previous: DeadJournal Dept.
 »
Next: Unlucky Monkey

Library Alpha Sort In Movable Type

| | Comments (0)

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"$>
</mt:entries>
</mt:loop>

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.

« 
Previous: DeadJournal Dept.
 »
Next: Unlucky Monkey

Leave a comment


Warning: Do not press "Preview" if you are replying to someone else's post. This will cause your message to be posted as a reply to the article itself.

Follow Me...

Subscribe  to feed Subscribe to this blog's feed

Follow me on Twitter

Friend me on Facebook

Friend me on Flickr

Also on LiveJournal

Read my stuff on
Profile

Twitter Updates

    [ Fetching ]

Monthly Archives

Powered by Movable Type 5.02
Bookmark and Share

About this Entry

This page contains a single entry by Serdar, published on September 29, 2009 12:39 PM.

» See all other entries for the month of September 2009.

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

Books I’ve Written


Tokyo Inferno

Evil stalks the streets of Tokyo, 1923, and will not rest until vengeance is found. Read a preview (PDF)  or buy a copy now! ($12 paperback / $20 signed)


The Four-Day Weekend

The “otaku novel”—about two guys who try to get away from it all, and end up taking it with them. Read a preview (PDF) or buy a copy now! ($12 paperback / $20 signed)


Summerworld

Fantasy meets psychology. A story of high adventure and deep insight in a place where desire reshapes the face of the world. Read a preview (PDF) or buy a copy now! ($12 paperback / $20 signed)

More of my writing.