Thanks for this module. The attached patch adds "An" to the array of leading articles to ignore; it worked for me.

The ability to add words to the ignored list through the module's admin settings would be extremely useful.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

domesticat’s picture

Status: Needs review » Needs work

This patch is a good idea, but it's replacing far too much text when it's just adding a couple of characters to one line in _views_natural_sort_filter_content()

brianbrarian’s picture

Yeah, I thought that might be the case, but I am a patch n00b and not a coder. I only posted what came out when I ran diff; as I said, it did the trick for me.

DanChadwick’s picture

I'm not sure of the status of this module's maintenance. I'm not using it personally, but I did look at it for ideas. In the interest of offering an improvement to both this and Numeric Sort - 546794 I offer you the following (untested) code. You'll find this in views_natural_sort.module, line 95.

// Original
function _views_natural_sort_filter_content($content) {
  return preg_replace(array(
    '/^(The|A)\s/i',
    '/\s(and|of|or)\s/i',
  ), array(
    '',
    ' ',
  ), $content);
}

// Revised
function _views_natural_sort_filter_content($content) {
  $content = preg_replace(array(
    '/^(The|A|An|)\s/i',
    '/[\#\'\"\(\)]/',
    '/\s(and|of|or)\s/i',
  ), array(
    '',
    '',
    ' ',
  ), $content);
  return preg_replace_callback(
    '/\d+/',
    create_function(
      '$matches',
      'return sprintf("%02u", strlen($matches[0])) . $matches[0];'
    ),
    $content
  );
}

I haven't tested this, except that I'm using essentially the same code in my module, and it works there. The changes are:

  1. A leading "An" and trailing space will be removed from the front.
  2. Some punctuation will be removed: # ' " ( ) Others could be added if desired.
  3. Strings of digits are prefixed with the number of digits. This will make positive integers sort in the natural order, e.g. 4 10 500 2000 would normally sort alphabetically 10 2000 4 500. It will now sort alphabetically 014 0210 03500 042000. Be aware that the string will no longer be human readable, although the purpose of the filtered field is only for sorting and grouping. As written, this will not handle numbers of varying lengths that have leading zeros.

I hope this helps someone. Maybe it could make it into the module if it tests ok.

heatherann’s picture

FileSize
424 bytes

Here is a patch which extends Views Natural Sort to filter out 'An' as well as the common French determiners (Le, La, L', Un, Une).

heatherann’s picture

FileSize
418 bytes

Whoops, that was a patch of a patch. Here's the same patch that applies to the original module file.

neclimdul’s picture

Looks very reasonable. Does this still need work?

heatherann’s picture

I'm happy with it. I'm using it on a multilingual site and it's working beautifully. Can it go in the next release of this module?

neclimdul’s picture

Status: Needs work » Needs review

Definitely

ricky.ybarra’s picture

@DanChadwick... works great. Thanks for sharing!

3CWebDev’s picture

@DanChadwick - Thanks!! I spent quite a while researching how to sort a View alpha numerically on Node titles. I was surprised there was more chatter on this topic. I'm glad to have found this module and your mod to sort numerically. Works like a charm!

generalredneck’s picture

This is my personal opinion of this feature request... I think a "word" list should be provided for each language so that the correct words are replaced in the regex. Also we could switch based on the language we are in so that the regex won't be HUGE. I think this gears for "multilingual" support... at least with the patch in #5.

The patch will be in the next release, however, lets open up another issue for "multilingual" support to keep track of the "(Le, La, L', Un, Une)" additions.

@neclimdul what do you think?

DanChadwick’s picture

@generalredneck Take care with international. Many of those ignore-worthy words from foreign languages should also be ignored in the site's language. Just because my site is in English doesn't mean that I don't get content containing ignore words from other languages.

So, for example, La might need to be the ignore list for just about every language.

generalredneck’s picture

@DanChadwick
Good, call... I think this feature needs more thought with that said. Maybe even taking this a little different direction than I initially thought.

Very valuable input though.

Thanks!

generalredneck’s picture

@ DanChadwick in #3
Interesting solution, but I'm not sure that it would work for decimals, as it would place precedence over decimals that are longer... for instance

Say I had the numbers 3.1101 3.14, 3.121... the preg_replace would do something like...
013.041101 013.0214 013.03121 which would order them such that they are displayed as...
3.14, 3.121 3.1101

Very creative solution for sorting integers though. kudos indeed.

generalredneck’s picture

Status: Needs review » Closed (fixed)

The patch was applied and will be available in the next version (6.x-1.1 & 7.x-1.1). Until then, you should be able to use the development version to get this fix. I'm in the process of closing out other tickets for the 1.1 release of both 6.x and 7.x.

3CWebDev’s picture

#3 mod by DanChadwick works on the D7 version too! Thanks again!