http://qs321.pair.com?node_id=1192610


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:  <%-{-{-{-<