Library Alpha Sort In Movable Type (Genji Press)

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"$>
</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.


Tags: Movable Type programming


comments powered by Disqus

About this Entry

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

You can see alphabetical or chronological listings of all entries in this category.

» See other Uncategorized / General entries for the month of September 2009.

» 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.

About Me

I'm an independent SF and fantasy author, technology journalist, and freelance contemplator for how SF can be made into something more than just a way to blow stuff up.

My Goodreads profile.

Learn some more about me.

My Books

Coming Soon

Out Now

More of my books

Search This Site


Other People We Like

Fandom

Archives