Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Changing ASCII collating sequence for sort

by tybalt89 (Monsignor)
on Apr 17, 2022 at 18:52 UTC ( [id://11143027]=note: print w/replies, xml ) Need Help??


in reply to Changing ASCII collating sequence for sort

Like this?

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11143026 use warnings; use List::AllUtils qw( sort_by ); print <<'EOF'; .,-;:!?"'`_#$%&*+/|=@\^~()<>[]{}0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmN +nOoPpQqRrSsTtUuVvWwXxYyZz intended sequence !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcde +fghijklmnopqrstuvwxyz{|}~ natural ASCII sequence EOF my @list = qw{ "Hello" Abel (hello) {adieu} @adieu [goodbye] Charlie ^ +Charlie ~Adieu zebra 21708 baker . - ; : ! ? " ' ` _ }; my @normalsorted = sort @list; print "@normalsorted\n\n"; my @newsorted = sort_by { tr/ .,\-;:!?"'`_#$%&*+\/|=@\\^~()<>[]{}01234 +56789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz/ -~/r } @li +st; print "@newsorted\n\n";

Outputs:

.,-;:!?"'`_#$%&*+/|=@\^~()<>[]{}0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmN +nOoPpQqRrSsTtUuVvWwXxYyZz intended sequence !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcde +fghijklmnopqrstuvwxyz{|}~ natural ASCII sequence ! " "Hello" ' (hello) - . 21708 : ; ? @adieu Abel Charlie [goodbye] ^C +harlie _ ` baker zebra {adieu} ~Adieu . - ; : ! ? " "Hello" ' ` _ @adieu ^Charlie ~Adieu (hello) [goodbye] { +adieu} 21708 Abel baker Charlie zebra

Replies are listed 'Best First'.
Re^2: Changing ASCII collating sequence for sort
by ikegami (Patriarch) on Apr 18, 2022 at 14:06 UTC

    The same approach could be used with the fabulous Sort::Key collection of modules.

    use Sort::Key qw( keysort ); my @newsorted = keysort { tr/.../.../r } @list;
Re^2: Changing ASCII collating sequence for sort
by etj (Deacon) on Apr 17, 2022 at 19:07 UTC

      I did, and don't call me Luke.</Airplane>

      (how do you think "sort_by" works internally?)

        I've never looked at the source of sort_by, but from the description I always assumed it was a Schwartzian transform. I'm deeply sorry my movie reference was unclear.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found