Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to store a title in a database to allow sorting without the leading "The", "A"

I run (and am presently completely overhauling) a website that deals with theater (njtheater.com if you're interested).

When I query a list of plays from the database, I'd like "The Merchant of Venice" to sort under the "M"s. Of course, when I display the name of the play, I need the "The" in front.

What the best way of designing the database to handle this?

(I'm using MS-SQL 2000)

like image 606
James Curran Avatar asked Nov 10 '08 18:11

James Curran


2 Answers

You are on the right track with two columns, but I would suggest storing the entire displayable title in one column, rather than concatenating columns. The other column is used purely for sorting. This gives you complete flexibility over sorting and display, rather than being stuck with a simple prefix.

This is a fairly common approach when searching (which is related to sorting). One column (with an index) is case-folded, de-punctuated, etc. In your case, you'd also apply the grammatical convention of removing leading articles to the values in this field. This column is then used as a comparison key for searching or sorting. The other column is not indexed, and preserves the original key for display.

like image 75
erickson Avatar answered Oct 27 '22 04:10

erickson


Store the title in two fields: TITLE-PREFIX and TITLE-TEXT (or some such). Then sort on the second, but display the concatenation of the two, with a space between.

like image 34
dkretz Avatar answered Oct 27 '22 02:10

dkretz