In Craft 2.5, how can you sort the entry index page by a custom field? 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 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?
Currently, the only way to add custom fields to the sortable attributes is to write a custom plugin which makes use of the modifyEntrySortableAttributes hook, e.g.:

public function modifyEntrySortableAttributes(&amp;$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 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!