Answers.Body: <p>Currently, the only way to add custom fields to the sortable attributes is to <a href="https://craftcms.com/docs/plugins/introduction">write a custom plugin</a> which makes use of the <a href="https://craftcms.com/docs/plugins/hooks-reference#modifyEntrySortableAttributes"><code>modifyEntrySortableAttributes</code></a> hook, e.g.:</p>
  
  <pre><code>public function modifyEntrySortableAttributes(&amp;$attributes)
  {
      $attributes['yourCustomFieldHandle'] = Craft::t('Label');
      $attributes['anotherCustomField'] = Craft::t('Another label');
  }
  </code></pre>
  
  <p>The <code>modifyEntrySortableAttributes</code> method would go into your plugin's primary class.</p>
  
  <p>Apart from custom fields, it's also possible to add other attributes in this manner, such as <code>authorId</code> (to sort on author) or <code>typeId</code> (to sort on entry type).</p>
  
  <p>It's also possible to <em>remove</em> a sortable attribute:</p>
  
  <pre><code>unset($attributes['postDate']);
  </code></pre>
  
  <p>There are corresponding hooks for adding sortable attributes to category, asset and user index tables, see <a href="https://craftcms.com/docs/plugins/hooks-reference#element-hooks">the official docs</a> for details.</p>
  
  <p>One gotcha to note is that since the sortable attributes are <em>source agnostic</em> (i.e. they're the same across all sections), any custom field you add to the sortable attributes should ideally exist for <em>all your entry types</em>, as attempting to sort on a custom field that doesn't exist for the entries in the current index table will throw an error.</p>
  
  <p>Moving into opinionated waters here, but to close: While the Customizable Element Indexes (CEI) is a great thing to have in core (and with a brilliant UI), I personally feel there's room for improvement; especially pertaining to sortable attributes. On that note, there are a few relevant feature requests:</p>
  
  <ul>
  <li><a href="http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/7274973-add-a-source-parameter-to-modifyentrysortableattr">http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/7274973-add-a-source-parameter-to-modifyentrysortableattr</a></li>
  <li><a href="http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/10943841-sortable-custom-columns">http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/10943841-sortable-custom-columns</a></li>
  <li><a href="http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/11533941-sort-filter-entries-by-entry-type">http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/11533941-sort-filter-entries-by-entry-type</a></li>
  <li><a href="http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/10579029-add-custom-fields-in-customizable-element-indexes">http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/10579029-add-custom-fields-in-customizable-element-indexes</a></li>
  </ul>
  
  <p><strong>EDIT</strong></p>
  
  <p>I just published a new plugin which will add any (sortable) custom field or meta value in any given element index to the index' sortable attributes:   <a href="https://github.com/mmikkel/CpSortableCustomColumns-Craft">https://github.com/mmikkel/CpSortableCustomColumns-Craft</a><br>
  The plugin is source aware, so it should avoid the issue with sorting on non-existing fields (as described above) in most cases. Like most Control Panel-specific plugins, it's basically one big hack – but it works until we can have better sorting abilities in core. Hopefully it's useful to someone!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-01T04:11:02.243Z
Answers.Text: Excellent answer, thanks Mats! Bonus points for the DIY plugin! :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-04T05:54:11.697Z
Answers.Text: Is it possible to sort by category using modifyEntrySortableAttributes? If not, is it possible some other way?
Answers.UserDisplayName: iRector
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-30T00:47:30.727Z
Answers.LastActivityDate: 2016-01-31T14:27:26.663Z
Answers.LastEditDate: 2016-01-31T14:27:26.663Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I recently updated a very large site to Craft 2.5. One of the great new features is the ability to <strong>view custom fields as columns</strong> on the entry index page.</p>
  
  <p>Previously, I used the <a href="https://github.com/mmikkel/DashCols-Craft" rel="nofollow noreferrer">DashCols</a> plugin to view those columns, but DashCols was rendered obsolete by the Craft 2.5 release. However, there was one other feature that DashCols has, which Craft 2.5 <em>doesn't</em> have...</p>
  
  <p><strong>How can you sort by custom fields on the entry index page?</strong></p>
  
  <p><a href="https://i.sstatic.net/nwSb0.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nwSb0.png" alt="Entry Index Page"></a></p>
  
Tags: <entries><sorting>
Title: In Craft 2.5, how can you sort the entry index page by a custom field?
ThreadText: # In Craft 2.5, how can you sort the entry index page by a custom field?
  
  (Asked by: username_0 on 2016-01-29)
  
  I recently updated a very large site to Craft 2.5. One of the great new features is the ability to **view custom fields as columns** on the entry index page.
  
  Previously, I used the [DashCols](https://github.com/mmikkel/DashCols-Craft) plugin to view those columns, but DashCols was rendered obsolete by the Craft 2.5 release. However, there was one other feature that DashCols has, which Craft 2.5 *doesn't* have...
  
  **How can you sort by custom fields on the entry index page?**
  
  [![Entry Index Page](https://i.sstatic.net/nwSb0.png)](https://i.sstatic.net/nwSb0.png)
  
  **Tags:** entries, sorting
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-01-30. Score: 12 | Accepted answer
  
  Currently, the only way to add custom fields to the sortable attributes is to [write a custom plugin](https://craftcms.com/docs/plugins/introduction) which makes use of the [`modifyEntrySortableAttributes`](https://craftcms.com/docs/plugins/hooks-reference#modifyEntrySortableAttributes) hook, e.g.:
  
  ```
  public function modifyEntrySortableAttributes(&$attributes)
  {
      $attributes['yourCustomFieldHandle'] = Craft::t('Label');
      $attributes['anotherCustomField'] = Craft::t('Another label');
  }
  
  ```
  
  The `modifyEntrySortableAttributes` method would go into your plugin's primary class.
  
  Apart from custom fields, it's also possible to add other attributes in this manner, such as `authorId` (to sort on author) or `typeId` (to sort on entry type).
  
  It's also possible to *remove* a sortable attribute:
  
  ```
  unset($attributes['postDate']);
  
  ```
  
  There are corresponding hooks for adding sortable attributes to category, asset and user index tables, see [the official docs](https://craftcms.com/docs/plugins/hooks-reference#element-hooks) for details.
  
  One gotcha to note is that since the sortable attributes are *source agnostic* (i.e. they're the same across all sections), any custom field you add to the sortable attributes should ideally exist for *all your entry types*, as attempting to sort on a custom field that doesn't exist for the entries in the current index table will throw an error.
  
  Moving into opinionated waters here, but to close: While the Customizable Element Indexes (CEI) is a great thing to have in core (and with a brilliant UI), I personally feel there's room for improvement; especially pertaining to sortable attributes. On that note, there are a few relevant feature requests:
  
  * <http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/7274973-add-a-source-parameter-to-modifyentrysortableattr>
  * <http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/10943841-sortable-custom-columns>
  * <http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/11533941-sort-filter-entries-by-entry-type>
  * <http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/10579029-add-custom-fields-in-customizable-element-indexes>
  
  **EDIT**
  
  I just published a new plugin which will add any (sortable) custom field or meta value in any given element index to the index' sortable attributes: <https://github.com/mmikkel/CpSortableCustomColumns-Craft>  
  The plugin is source aware, so it should avoid the issue with sorting on non-existing fields (as described above) in most cases. Like most Control Panel-specific plugins, it's basically one big hack – but it works until we can have better sorting abilities in core. Hopefully it's useful to someone!
  
  > **Comments:**
  > **username_0**: Excellent answer, thanks username_1! Bonus points for the DIY plugin! :)  
  > **username_2**: Is it possible to sort by category using modifyEntrySortableAttributes? If not, is it possible some other way?  
  
  ---
  
  