Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Sort::Key::Natural sorting discrepancy

by Laurent_R (Canon)
on Nov 01, 2017 at 10:00 UTC ( [id://1202502]=note: print w/replies, xml ) Need Help??


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

Hi cr8josh,

If I understand well what you want, this might be your solution:

use strict; use warnings; use feature 'say'; my @list = qw(P007b_Yum P007_Yum P007B_YUM P007_YUM P007b_yum P007_yum +); my @sorted = sort { lc $a cmp lc $b } sort { $b cmp $a } @list; say for @sorted;
This produces the following output:
$ perl sort_lc.pl P007_yum P007_Yum P007_YUM P007b_yum P007b_Yum P007B_YUM
The first sort (on the right-hand side) puts the list in the right order with respect to words having the same letters but in different cases, the second sort (on the left) then deals with underscores versus letters. This works because the sort algorithm used (merge sort) is stable, so that items that compare equal when changed to lower case (second sort) are kept in the same order as determined by the first sort.

If this is still not the order you want, please let us know what you expect.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-16 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found