How to use Views Natural Sort 2.0

Using the Module

Step 1: Install Views Natural Sort Module

If you don't know how to do this, checkout the community documentation: Installing contributed modules. Using drush, it's as simple as going to your install directory and typing:
drush dl views_natural_sort -y; drush en views_natural_sort -y

Step 2: Modify Transformation Settings

You may find that the default settings are not exactly what you need. Navigate to Structure > Views > Settings > Natural Sort Settings or the url admin/structure/views/settings/views_natural_sort. Follow the directions presented in for each transformation setting. When you are done hit "Save Settings" and your node titles will be re-indexed for you automatically.

Step 3: Create The View for Sorting Node Titles

To create the view, the Views UI module must be enabled. I usually start with some defaults: Next add the Sort Criteria "Content: Title". When you do, you will be asked how you would like the item to be sorted. Choose "Sort ascending naturally" or "Sort descending naturally" depending on your use-case. Apply the changes.

Save the view and check out the results.

Step 4: Enable Sorting for Fields

Enable the Views Natural Sort Text Field Support module.

Step 5: Enable Sorting for Specific Fields

Navigate to manage a field's settings. This is typically Structure > Content Types > manage fields > edit

Step 6: Edit View for Natural Sorting by Field

You can follow the same steps in Step 3 pretty much. Find the field's Sort Criteria and choose "Sort ascending naturally" or "Sort descending naturally" depending on your use-case.

Development and Extending Tips

I get several requests for adding features on how to "transform" the data so that it sorts naturally for a very specific situation, like skus or specially formatted dates. With that said, I have built a framework that allows you to add, rearrange, or even remove transformations on fields.

High Level Technical Understanding of How the Module Works

The gist of this module is that it takes string data and transforms it in such a way to trick database string sorting into sorting a displayed value in a different order. The easiest situation is book titles sorted in the library. A more complicated scenario are numbers or dates. Technically, the following process happens:
  • An Entity is saved.
  • On the various entity hooks, a call is made to take the string and run it through our transformations. If you are creating your own natural sort on a new field, you will have to implement this step by calling views_natural_sort_store.
  • The list of transformations are built. This can be manipulated with hook_views_natural_sort_transformations_alter.
  • The transformed string and other information to identify what that string belongs to are stored in the views_natural_sort table in the database. An example of a transformed string would be something like "The 10 apples" becomes "0210.0 apples" which removes the word "The" and transform the number in such a way that if the next string had a number in the same position, it would be sorted in numerical order. Example 10, 20, 100 instead of 10, 100, 20.
  • In the view, on sort, the views_natural_sort table is joined to the query and then sorted by the 'content' field which is the transformed data.
You can take a look at views_natural_sort.api.php to get an idea of the hooks you can use. To create your own natural sort situation There are a couple of hooks that are required for the module to know how to find data that should be sorted naturally. Those hooks are called in the "Index Rebuild". Index Rebuild happens any time settings are changed.