<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Push cx &#187; sorting</title>
	<atom:link href="http://push.cx/tag/sorting/feed" rel="self" type="application/rss+xml" />
	<link>http://push.cx</link>
	<description>A traveling geek&#039;s blog on development, games, and the web</description>
	<lastBuildDate>Fri, 14 Oct 2011 10:24:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Alphabetical Sorting SQL Without &#8220;The&#8221;</title>
		<link>http://push.cx/2007/alphabetical-sorting-sql-without-the</link>
		<comments>http://push.cx/2007/alphabetical-sorting-sql-without-the#comments</comments>
		<pubDate>Tue, 01 May 2007 21:10:30 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[alphabetization]]></category>
		<category><![CDATA[lexical sorting]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://push.cx/2007/alphabetical-sorting-sql-without-the</guid>
		<description><![CDATA[Chris McAvoy asked how to sort alphabetically so that entries starting with &#8220;A&#8221;, &#8220;An&#8221;, or &#8220;The&#8221; end up in the proper place instead of jumbled into the As and Ts. Do I create a field “name_without_the” and select * from bands order by name_without_the? I’m assuming this is a common alpha-issue that’s been solved millions [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3dlYmxvZy5sb25lbHlsaW9uLmNvbS8yMDA3LzA0LzMwL3NxbC1hbHBoYWJldGljYWwtc29ydGluZy13aXRob3V0LXRoZQ==">Chris McAvoy asked</a> how to sort alphabetically so that entries starting with &#8220;A&#8221;, &#8220;An&#8221;, or &#8220;The&#8221; end up in the proper place instead of jumbled into the As and Ts.
</p>

<blockquote>
Do I create a field “name_without_the” and select * from bands order by name_without_the? I’m assuming this is a common alpha-issue that’s been solved millions of times, but I can’t find a best-practice suggestion via two minutes of Googling.
</blockquote>

<p>
Here&#8217;s the query I came up with to do it in-database:
</p>

<pre>&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> name <span style="color: #993333; font-weight: bold;">FROM</span> bands
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> CASE
  WHEN LOWER<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>name, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> =<span style="color: #ff0000;">"a "</span> THEN SUBSTRING<span style="color: #66cc66;">&#40;</span>name, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
  WHEN LOWER<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>name, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> =<span style="color: #ff0000;">"an "</span> THEN SUBSTRING<span style="color: #66cc66;">&#40;</span>name, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
  WHEN LOWER<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>name, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> =<span style="color: #ff0000;">"the "</span> THEN SUBSTRING<span style="color: #66cc66;">&#40;</span>name, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
  ELSE name
END;</pre>

<p>
The problem with this is that it&#8217;s sloooow &#8212; your database engine will have to look at every single row of the table instead of scanning an index. So if speed is an issue, you need to add another column, and you have two choices:
</p>

<p>
First, you could add a <kbd>name_article</kbd> column and split your data. For the band &#8220;The Who&#8221;, <kbd>name_article</kbd> would be &#8220;The&#8221; and <kbd>name</kbd> would be &#8220;Who&#8221;. You can sort by <kbd>name</kbd> but you&#8217;ll need to recombine the fields to use them. If you don&#8217;t have an ORM to nicely abstract this it&#8217;ll be painful. So while it saves duplication of data, it&#8217;s just a bad idea to make database columns depend on each other.
</p>

<p>
The better way to do it is Chris&#8217;s original idea: create a <kbd>name_without_article</kbd> column to store it. For the band &#8220;The Who&#8221;, <kbd>name_without_article</kbd> would be &#8220;Who&#8221; and <kbd>name</kbd> would be &#8220;The Who&#8221;. This will just be straightforward to deal with, and the extra space you use will be offset by the simpler and faster code you write.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=237" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/alphabetical-sorting-sql-without-the/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

