sciurius has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,
I want my perl program to sort just like the system it is running on.
This is trivial on Linux, but somehow seems complicated on Windows and macOS. I use the following test program
#! perl use strict; use warnings; use utf8; binmode STDOUT => ':utf8'; my @words = ( "Maria Lenore", "Mária Helena", "María Dolores" ); my @sorted = sort @words; if ( "@sorted" eq "María Dolores Mária Helena Maria Lenore" ) { print("Sorted OK\n"); } elsif ( "@sorted" eq "Maria Lenore María Dolores Mária Helena" ) { print("Sorted C (ASCII)\n"); } else { print("Sort failed: @sorted\n"); }
On Windows, I get
PS C:\Users\Johan> perl locale.pl Sorted C (ASCII) PS C:\Users\Johan> perl -Mlocale locale.pl Sorted C (ASCII) PS C:\Users\Johan>
Same on macOS.

When I create files with names "Maria Lenore", "Mária Helena" and "María Dolores" they sort correctly in the Explorer (Windows) and Finder (macOS).
How can I have perl use the same sort order as the system?

Windows 10 with Strawberry Perl 5.30
MacOS 10.15 with Perl 5.34.
These versions are a bit old, but I can not imagine I'm the first to try such a sort in 25+ years.

Replies are listed 'Best First'.
Re: Using system sort order
by Arunbear (Prior) on Aug 11, 2025 at 11:58 UTC
    On Linux, without the -Mlocale switch, I get the same behaviour that you get on Windows. To get the result you wish, I used Unicode::Collate e.g.
    #! perl use strict; use warnings; use utf8; use Unicode::Collate; binmode STDOUT => ':utf8'; my @words = ( "Maria Lenore", "Mária Helena", "María Dolores" ); my $col = Unicode::Collate->new(level => 1); my @sorted = $col->sort(@words); if ( "@sorted" eq "María Dolores Mária Helena Maria Lenore" ) { print("Sorted OK\n"); } elsif ( "@sorted" eq "Maria Lenore María Dolores Mária Helena" ) { print("Sorted C (ASCII)\n"); } else { print("Sort failed: @sorted\n"); }
    I had these bookmarked ages ago, but didn't look at them until today:
      Thanks for the suggestion! This does, indeed, give the desired results on both Windows and macOS.

      But it does not use the system settings...
      See e.g. https://youtu.be/j64acCeati4?t=548 .

      For the time being it is already a substantial improvement.
Re: Using system sort order
by 1nickt (Canon) on Aug 11, 2025 at 10:30 UTC

    I can confirm that the sort order on my Mac is what you have posted, but I'm surprised and I feel it's wrong. There's no way I can imagine that á should come before a or í before i with the LC_COLLATE value set to en_US.UTF-8. I think Perl is getting it right here.


    The way forward always starts with a minimal test.
      It isn't doing anything fancy like sorting all a variants together but sorting the within a by diacritic, it is treating the a's as equal and sorting these strings by the different second words.