in reply to Natural Sort / Windows sort problem

IIRC, Sort::Naturally implemented some tricks to be more like a file explorer, while Sort::Key::Natural is more pure.

If you want the file extension to be treated as an independent secondary sorting key, you can use Sort::Key::Maker for creating a multi-key sorting function:

use Sort::Key::Natural; use Sort::Key::Maker sort_filenames => sub { /^(.*?)((?:\.[^\.]*)?)$/ +}, qw(natural natural); my @filenames = qw(...); my @sorted = sort_filenames @filenames;

Replies are listed 'Best First'.
Re^2: Natural Sort / Windows sort problem
by cr8josh (Sexton) on Oct 31, 2017 at 15:54 UTC

    Thank you!

    I found something I don't understand, which is forcing me to use the Win32 call method described above. When trying to do a case-insensitive sort by using {uc $_} or {lc $_}, I get different and unexpected results. Here is an example using the same string sets, one upper case, one not.

    use feature qw( say ); use Sort::Key::Natural qw( natsort ); say for natsort qw( P007B_YUMYUMNOISESANDTASTING1 P007_YUMYUMNO +ISESANDTASTING5A P007_YUMYUMNOISESANDTASTING5B 007_YUMYUMNOISESANDTAS +TING5C ); say for natsort qw( P007b_YumYumNoisesAndTasting1 P007_YumYumNoises +AndTasting5A P007_YumYumNoisesAndTasting5B P007_YumYumNoisesAndTastin +g5C );

    Output:

    P007B_YUMYUMNOISESANDTASTING1 P007_YUMYUMNOISESANDTASTING5A P007_YUMYUMNOISESANDTASTING5B P007_YUMYUMNOISESANDTASTING5C P007_YumYumNoisesAndTasting5A P007_YumYumNoisesAndTasting5B P007_YumYumNoisesAndTasting5C P007b_YumYumNoisesAndTasting1

    In the first case, the "P007B_" sorts before "P007_". In the second case it sorts after. If I do all lc, it's the same thing, 7b sorts to the beginning instead of the end:

    P007b_yumyumnoisesandtasting1 P007_yumyumnoisesandtasting5a P007_yumyumnoisesandtasting5b P007_yumyumnoisesandtasting5c

    Anyone understand why? I need it to sort after, but be case insensitive...

    Thanks!