0

I use views to display a sorted list of book titles. Titles that begin with a quote are listed first. I want to ignore the quote for sorting purposes. So, instead of:

  • "Once Upon a Time": Fairy Tales in Reality
  • Grimm's Fairy Tales
  • The Best Book of Fairy Tales

I want:

  • Grimm's Fairy Tales
  • "Once Upon a Time": Fairy Tales in Reality
  • The Best Book of Fairy Tales

It appears the Views Natural Sort module will handle this, but it's still in Alpha for Drupal 8. Is there another, relatively straightforward (non-custom-code if possible) means of accomplishing this?

I've read a number of posts addressing more complex situations with fairly complex solutions, but it seems something this simple should have a simple solution.

  • 1
    Views Natural Sort seems to be that simple solution you're looking for. Where did you get stuck configuring it? If there were a simpler solution then wouldn't that module use it? – prkos Mar 20 at 22:19
  • Thanks @prkos. I shied away from it because it's still alpha. I read the documentation and it looked like it was somewhat complex. But I'll go ahead and give it a try and then report back here how it works for me. – Marshall Morrise Mar 20 at 23:03
  • @prkos By way of report, Views Natural Sort seems to work pretty well, except I haven't been able to get it to ignore curly quotes at the beginning of a title. I'll work on that some more and report again. – Marshall Morrise Mar 21 at 14:48
1

views_natural_sort is able to do this kind of sorting.

It might be currently in alpha state but it already has more than 250 installation for the alpha 3 release which means that most major bugs should have already been ironed out.

Upon enabling you are able to choose title to sort with and you are given 2 extra options for natural sorting (sort natually ascending and sort natually descending)

enter image description here

It also has some handy settings at admin/structure/views/settings/views_natural_sort which you will allow you to chose what other characters you want to ignore (and other settings)

Following are the default settings

enter image description here

  • Thanks for this suggestion, @GiorgosK. I set up Natural Sort and used the settings you mention. Even after that, I wasn't able to get quite what I needed because of the international characters in the titles I'm sorting. I'll post an answer to explain what I ended up doing.Maybe that will prompt you or someone to say, "Natural Sort could have done that as well" or to make another suggestion. – Marshall Morrise Mar 22 at 17:40
0

Thanks to all for the suggestions. After I got Natural Sort mostly working, I ran into problems using the sorted titles with Views grouping due to accented letters (no fault of Natural Sort). For example, I'd get a group with "A" as the group heading and another group with "Á" (accented A).

To solve this problem, I did the following:

  • Add a field named sort_title to the content type and hide it from users.
  • Add a node_presave() hook for the content type to a custom module.
  • Create a sortable_title function in the hook to
    • Replace accented letters and other international characters in the title with close English equivalents.
    • Force the entire title to uppercase.
    • Strip all non-alphanumeric characters from the front of the title.
    • Add a ~ character to titles that begin with a number (because I want them to sort after titles beginning with letters).
  • Save this sortable title in the sortable_title field for the node.
  • Use the sortable_title field for sorting and grouping in the view, instead of the regular title field, which I was using (and which is still the one that gets displayed).
  • I also subthemed the views-view-list.html.twig template to replace the ~ with # in the grouped heading (I had already subthemed the file for another reason anyway).

This approach isn't the most general, but it's giving me exactly the outcome I want, so I'm no longer using Natural Sort. It's only about 30 lines of code in the hook function (most of that being a table of international characters and their replacements) and of course, there's the hidden duplicate of the title field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.