Answers.Body: <p>You can't do this using the <em>craft.entries</em> tag in twig. You'll need to create a new function in your plugin that returns the entries in your custom order.</p>
  
  <p>Assuming you can create an array of element ids in your custom order then the following code should allow you to then fetch entries in that order:</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  
  // filter entries by element ids
  $criteria-&gt;setAttribute('id', $elementIds);
  
  // order entries by element ids
  $criteria-&gt;setAttribute('order', 'FIELD(elements.id, '.join(', ', $elementIds).')');
  
  // get entries
  $entries = craft()-&gt;elements-&gt;findElements($criteria);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T05:36:16.393Z
Answers.Text: Thanks Ben! I'm not sure if this is exactly what I'm looking for (although it sounds like what I'm looking for might not exist). I'm gonna try a few things based on your example, I'll let you know how it goes.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T08:43:14.003Z
Answers.Text: Ok, it looks like this is going to work well! I made a few tweaks (too much to explain here), but the core ideas came from this suggestion. Thanks Ben!
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T10:37:05.420Z
Answers.Text: good to hear. as is often the case with craft, there are many ways of achieving this based on your specific needs. this seemed like the cleanest to me.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-27T21:27:40.533Z
Answers.Text: Hey Ben, as you said "there are many ways of achieving this"... I just discovered [this method](http://craftcms.stackexchange.com/q/2786/45) which also worked great for me! It actually saved me a separate query, so I didn't have to figure out `$elementIds` separately.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-28T13:12:04.103Z
Answers.Text: very nice solution!!
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-25T07:54:52.187Z
Answers.LastActivityDate: 2014-09-25T07:54:52.187Z
Answers.UserDisplayName: Ben Croker
Body: <p>I have a plugin which contains a <strong>Record</strong>. My record contains a <strong>foreign key</strong> of the related <strong>element ID</strong>. The only other column in my custom table is <em>the value I'd like to sort by</em>.</p>
  
  <p>In Twig syntax, <strong>how can I expand on a normal Craft query to sort by a column in my custom table?</strong></p>
  
  <p>Ordinary query:</p>
  
  <pre><code>craft.entries.section('colors').find
  </code></pre>
  
  <p>Ordinary query which sorts by my custom data:</p>
  
  <pre><code>craft.entries.section('colors').myPlugin('asc').find
  </code></pre>
  
  <hr>
  
  <p><strong>Disclaimer:</strong></p>
  
  <p>I've done something similar to this once before... But in that case, I used the <code>modifyElementsQuery</code> method in my Field Type. Since this new plugin doesn't require a field type, I'm not exactly sure of the best way to modify a standard query.</p>
  
Tags: <plugin-development><queries><records>
Title: How can I expand on a normal Craft query to sort by a column in a custom table?
ThreadText: # How can I expand on a normal Craft query to sort by a column in a custom table?
  
  (Asked by: username_0 on 2014-09-23)
  
  I have a plugin which contains a **Record**. My record contains a **foreign key** of the related **element ID**. The only other column in my custom table is *the value I'd like to sort by*.
  
  In Twig syntax, **how can I expand on a normal Craft query to sort by a column in my custom table?**
  
  Ordinary query:
  
  ```
  craft.entries.section('colors').find
  
  ```
  
  Ordinary query which sorts by my custom data:
  
  ```
  craft.entries.section('colors').myPlugin('asc').find
  
  ```
  
  ---
  
  **Disclaimer:**
  
  I've done something similar to this once before... But in that case, I used the `modifyElementsQuery` method in my Field Type. Since this new plugin doesn't require a field type, I'm not exactly sure of the best way to modify a standard query.
  
  **Tags:** plugin-development, queries, records
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-09-25. Score: 5 | Accepted answer
  
  You can't do this using the *craft.entries* tag in twig. You'll need to create a new function in your plugin that returns the entries in your custom order.
  
  Assuming you can create an array of element ids in your custom order then the following code should allow you to then fetch entries in that order:
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  
  // filter entries by element ids
  $criteria->setAttribute('id', $elementIds);
  
  // order entries by element ids
  $criteria->setAttribute('order', 'FIELD(elements.id, '.join(', ', $elementIds).')');
  
  // get entries
  $entries = craft()->elements->findElements($criteria);
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks Ben! I'm not sure if this is exactly what I'm looking for (although it sounds like what I'm looking for might not exist). I'm gonna try a few things based on your example, I'll let you know how it goes.  
  > **username_0**: Ok, it looks like this is going to work well! I made a few tweaks (too much to explain here), but the core ideas came from this suggestion. Thanks Ben!  
  > **username_1**: good to hear. as is often the case with craft, there are many ways of achieving this based on your specific needs. this seemed like the cleanest to me.  
  > **username_0**: Hey Ben, as you said "there are many ways of achieving this"... I just discovered [this method](http://craftcms.stackexchange.com/q/2786/45) which also worked great for me! It actually saved me a separate query, so I didn't have to figure out `$elementIds` separately.  
  > **username_1**: very nice solution!!  
  
  ---
  
  