in reply to Re^2: Sorting path names with Sort::Key
in thread Sorting path names with Sort::Key

I would agree if this produced output like keysort but with better number handling, but because the null characters are treated equivalently to any other non-word characters, and natsort splits the words on these, the output differs more than just on numbers. Note that 'abc-def' now sorts before '/abc/def' in the output:
Ordered with keysort: /abc /abc/1/2/a/b/c /abc/2 /abc/22 /abc/3 /abc/def/1/2/a/b/c /abc!def/1/2/a/b/c /abc-def/1/2/a/b /abcdef/1/2/a/b/c /usr/a Ordered with natkeysort: /abc /abc/1/2/a/b/c /abc/2 /abc/3 /abc/22 /abc-def/1/2/a/b /abc/def/1/2/a/b/c /abc!def/1/2/a/b/c /abcdef/1/2/a/b/c /usr/a

Replies are listed 'Best First'.
Re^4: Sorting path names with Sort::Key
by salva (Canon) on Jan 23, 2025 at 13:28 UTC
    Sort::Key::Natural provides a low level function mkkey_natural for that:
    # untested use Sort::Key qw(keysort); use Sort::Key::Natural qw(mkkey_natural); @sorted = keysort { join "\x00\x00", map mkkey_natural, split /\//, $_ + } @paths;
    The double 0 char (\x00\x00) is used for joining because mkkey_natural uses a similar trick in the generated keys.