How to use Views Natural Sort 2.x

Last updated on
11 March 2021

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

This documentation needs work. See "Help improve this page" in the sidebar.

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

Note:
This step has changed from the alpha versions.

You may find that the default settings are not exactly what you need. Only local images are allowed. 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.
 

Note:
With the stable release, more fields can be sorted naturally than just "Content: Title". Any text property (such as User: Name, File: Name, can also be naturally sorted. The criteria is that the property must be of type varchar and not have a special sort handler in Views.

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

Note:
Selecting this setting effects all instances of that field, much the same way carnality does. For instance, if you have this field on a user as well as a content type, then the user will be indexed for natural sorting as well.

Technical Understanding:
Because the fields module won't let you sort fields with unlimited text length, this module follows that same pattern. With that said, this option will not be available on text fields that do not have a set text length.

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.

Technical Understanding:
Views automatically groups fields with carnality that is higher than 1. With that said, there is an issue reported over at the views issue queue about duplicate rows happening in the case that you sort. This is the way the field handler cheats when sticking all of a field's values together, thus when you sort, produces behavior you weren't expecting. I am looking to see if I can find a "solution" to that for natural sorting.

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.

Note:
When changing settings programmatically, manipulating transformations, or adding new fields, it is imperative that you rebuild the views_natural_sort table. If you have defined your hooks correctly, this can be achieved simply by calling views_natural_sort_rebuild_index_submit();

Help improve this page

Page status: Needs work

You can: