title: Customized sort in NSFetchedResultsController
body: <p>After spending hours, I found that it was not possible to do customized sort in NSFetchedResultsController backed by SQLite from following article.</p>
  
  <p><a href="https://stackoverflow.com/questions/4789782/nsfetchedresultscontroller-custom-sort-not-getting-called">NSFetchedResultsController custom sort not getting called</a></p>
  
  <p>However, I couldn't find actual solution for my problem.</p>
  
  <p>Here's what I am trying to do.</p>
  
  <p><strong>Background:</strong><br>
  I have a English dictionary database (just a simple list of words - very large) in CoreData. Words are shown in UITableView using NSFetchedResultsController.</p>
  
  <p>UITableView has an associated Search Bar. When an user inputs a string in Search Bar, UITableView shows a list of words filtered.</p>
  
  <p><strong>Why do I need customized sort:</strong><br>
  When a user inputs a string, let's say it was <code>bre</code>, I change it to regular expression, <code>b.*r.*e.*</code> and use it as a NSPredicate, then, do performFetch. So that all words like 'bare' and 'break' will be shown in the table view.</p>
  
  <p>By default, the words are shown as a alphabetical order. Therefore, <code>bare</code> will come before <code>break</code>.</p>
  
  <p>I want <code>break</code> to come before <code>bare</code> on the searched list because the first three character of <code>break</code> exactly matches to what the user input. </p>
  
  <p><strong>Possible ideas:</strong><br></p>
  
  <ol>
  <li>Copy the result of NSFetchedResultsController into NSArray and do the custom sort.
  I am not sure how fast NSArray will work for a large array like an English dictionary.</li>
  <li>Try performFetch multiple times. For example above, try performFetch for <code>bre.*</code>, <code>br.+e.*</code>, <code>b.+r.+e.*</code> in order and combine them.</li>
  </ol>
  
  <p>Both ideas don't look very neat.</p>
  
  <p>I appreciate if you can suggest any known neat &amp; typical solution for this kind of problem.</p>