Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Sort::Key::Natural sorting discrepancy

by salva (Canon)
on Nov 01, 2017 at 13:20 UTC ( [id://1202522]=note: print w/replies, xml ) Need Help??


in reply to Sort::Key::Natural sorting discrepancy

As tangent has already explained in his post, Sort::Key::Natural breaks the string at the boundaries between alphabetic and number groups. So for instance, P007b_Yum is equivalent to P_7_b_Yum.

If you want alphanumeric groups to remain unbroken, you will have to change the sorting-key generation algorithm.

This probably does what you want (untested):

use Sort::Key qw(keysort); sub mknkey { my $n = shift; $n =~ s/^0+//; my $len = length $n; my $nines = int ($len / 9); my $rest = $len - 9 * $nines; ('9' x $nines) . $rest . $n; } sub mknatkey { my $nat = @_ ? shift : $_; $nat =~ s/(\d+)/mknkey($1)/ge; $nat; } my @sorted = keysort { mknatkey($_) } @data;

Replies are listed 'Best First'.
Re^2: Sort::Key::Natural sorting discrepancy
by 1nickt (Canon) on Nov 01, 2017 at 14:01 UTC

    Hi salva,

    Thanks for clarifying. Now that I know from your follow-up what the truth is, I can discern it in the docs. However, I humbly submit that the doc could be clearer.

    I misread the existing doc and concluded (encouraged in the wrong conclusion by the OP's code) that the string was split only on the underscores etc. (i.e.: 'boundaries' == 'underscores, etc.'). That's mostly my fault, for only seeing what I thought I was looking for, but here's a suggestion: place next to each other the descriptions of the two ways in which the string could be split, and highlight that alpha words and numbers are different, not a group:

    "Under natural sorting, strings are split at word and number boundaries, and the resulting ... "

    Possibly better as:

    "Under natural sorting, strings are split at boundaries between words and numbers and at non-alphanumeric characters (see below), and the resulting ..."

    Let me know if a patch / PR would be helpful, if you think this would be an improvement.

    Thanks for all you do for Perl!


    The way forward always starts with a minimal test.
      Following your advice, I have changed the description on the development version of the module.
Re^2: Sort::Key::Natural sorting discrepancy
by cr8josh (Sexton) on Nov 01, 2017 at 15:56 UTC

    Many many thanks to all who chimed in. Apologies my goal was not clear! My high level goal is to reproduce Windows Explorer's file sorting. The only solution that worked for me consistently was calling Win32::API which I preferred not to do...

    Salva, you nailed it, I needed alphanumeric groups to sort unbroken, and your solution works perfectly. Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202522]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-23 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found