Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Sort undef

by AnomalousMonk (Bishop)
on Jun 12, 2017 at 15:44 UTC ( #1192610=note: print w/replies, xml ) Need Help??


in reply to Re^2: Sort undef
in thread Sort undef

Another slight variation on a custom sort sub:

c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; my $OptOrd = 1; ;; my $Test = [ [ 1, 'c' ], [ 2, 'a' ], [ 3, '' ], [ 4, undef ], [ 5, 'foo' ], [ 6, '' ], [ 7, 'cee' ], [ 8, undef ], [ 9, 'tee' ], [ 10, 't' ], ]; ;; my $Expected = [ [ 3, '' ], [ 6, '' ], [ 2, 'a' ], [ 1, 'c' ], [ 7, 'cee' ], [ 5, 'foo' ], [ 10, 't' ], [ 9, 'tee' ], [ 4, undef ], [ 8, undef ], ]; ;; my @got = sort by_deaccented_ascending_with_undef_highest @$Test; ;; is_deeply \@got, $Expected, 'custom sort sub'; ;; done_testing; ;; sub deaccent { return $_[0]; } ;; sub by_deaccented_ascending_with_undef_highest { my ($aa, $bb) = ($a->[$OptOrd], $b->[$OptOrd]); ;; return defined $aa && defined $bb ? deaccent($aa) cmp deaccent($bb) : defined $bb cmp defined $aa ; } " ok 1 - custom sort sub 1..1 ok 2 - no warnings 1..2
defined returns '' (empty string) for undefined, 1 for defined, and '' will lexically cmp below 1.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-01-19 17:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (55 votes). Check out past polls.

    Notices?