Alphabetic title sorting in Movable Type

February 13, 2006

Return to blog homepage

A client of ours recently asked us to complete an interesting project with their MovableType powered Weblog. The client, Armchair Interviews. They provide book reviews on a variety of subjects and wanted to have the ability to sort their archives using academic sorting.

Academic sorting doesn't take into account the, an and and when sorting a book title. For instance if you have The DaVinci Code, Freakonomics and Blink in your library, they would be academically sorted as follows:

  • Blink
  • DaVinci Code, The
  • Freakonomics

By default, MovableType allows you to sort alphabetically, but there is no built-in way to go about sorting academically. Luckily, MovableType has a powerful plugin architecture that let's developers add functionality to the platform. Two such plugins proved very useful to this project: Stepan Riha's Collate and Brad Choate's RegEx.

Collate allows you to build a virtual collection of records based on a set of criteria. This is useful if you want to sort or organize data beyond the basic date and time sorting options built-in to the MovableType platform. You could, for example, grab the last twenty entries in your blog and set respective fields for the date, time, title and url for each entry. You could then sort by any of those fields.

The RegEx plugin brings the power of regular expressions to MovableType. A Regular expression is a set of matching rules that you compare against a string. This powerful text processing opens up limitless possibilities. Assume you want to search the word cat for certain criteria. If you wanted to search an HTML page for all instances of the <P> tag and wanted to convert it to lowercase, you can use the following regex <P\b[^>]*>(.*?)</P> as your find and <p\b[^>]*>(.*?)</p> as your replace. Regular expressions eliminate a lot of the mundane tasks that can plague computer users. Not only that, but it makes it fairly easy to implement academic sorting on a MovableType archive.

The process involved collating and sorting each entry based on the title. Then using a <MTIfMatches> decision structure, we check the EntryTitle for a regex pattern. We set patterns to check for the existence of the, and and an. If the title begins with those words, we strip it from the title and then append it to the end. If none of those criteria are matched, we output the normal title. You can see our code here.

<MTCollateCollect>
<MTEntries lastn="9999">
<MTCollateRecord>

<MTIfMatches var="EntryTitle" pattern="m/(^A )/">
<MTCollateSetField name="title"><$MTEntryTitle regex="s/(^A )//g"$>,
A</MTCollateSetField>
</MTIfMatches>

<MTIfMatches var="EntryTitle" pattern="m/(^An )/">
<MTCollateSetField name="title"><$MTEntryTitle regex="s/(^An )//g"$>,
An</MTCollateSetField>
</MTIfMatches>
<MTIfMatches var="EntryTitle" pattern="m/(^The )/">
<MTCollateSetField name="title"><$MTEntryTitle regex="s/(^The )//g"$>,
The</MTCollateSetField>
</MTIfMatches>

<MTIfNotMatches var="EntryTitle" pattern="m/(^The |^A |^An )/">
<MTCollateSetField name="title"><$MTEntryTitle$></MTCollateSetField>
</MTIfNotMatches>

<MTCollateSetField name="link"><$MTEntryPermalink$></MTCollateSetField>
<MTCollateSetField name="excerpt"><$MTEntryExcerpt$></MTCollateSetField>

</MTCollateRecord>
</MTEntries>
</MTCollateCollect>

As you can see, the code is fairly simple. Like many problems involving technology the hardest part is finding the solution. Implementing it isn't nearly as difficult. Hopefully this will prove useful to other developers who find themselves faced with the same situation.

One issue we did run into was not taking into account the space between words. When we initially stripped the leading word from certain titles, it retained the space before the next word. This caused problems with our sorting because the space is taken into account. Modifying each regular expression to strip that leading space solved the problem.

If you have any questions or comments, please let us know.

Justin Williams is a MovableType platform expert who also moonlights as a technical writer for MacZealots.com.

Comments

Post a comment

Remember Me?

(you may use HTML tags for style)