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


in reply to Re: Sort undef
in thread Sort undef

> wait this might go wrong if cmp_undef("",undef) returns 0. (Not sure)

actually it works undef is not cast to "" and hence less than empty string°

but it seems you didn't sue strictures, that's why I needed to add no warnings 'uninitialized';

> So you'd need to treat the 3 extra cases for $a, $b being undef and return -1,0,1 accordingly.

see cmp_undef2 for that solution

use strict; use warnings; use Data::Dump; sub cmp_undef1 { return $a cmp $b if defined $a and defined $b; no warnings 'uninitialized'; return $b cmp $a; # invert order otherwise } sub cmp_undef2 { if (defined $a) { if (defined $b) { $a cmp $b; } else { -1 } } else { if (defined $b) { 1 } else { 0 } } } my @array =( (undef)x 3, qw/c b a/, ("")x3); my @result = sort cmp_undef1 @array; dd \@result; @result = sort cmp_undef2 @array; dd \@result;

output

["", "", "", "a", "b", "c", undef, undef, undef] ["", "", "", "a", "b", "c", undef, undef, undef]

I don't want to elaborate on the sort SUBNAME LIST syntax see sort for details and other variations. (TIMTOWTDI I chose the shortest)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

°) which is strange because

DB<1> p "" eq undef 1

Replies are listed 'Best First'.
Re^3: Sort undef
by choroba (Archbishop) on Jun 12, 2017 at 13:55 UTC
    > actually it works undef is not cast to "" and hence less than empty string

    No, it works because sort is stable and you declared the lucky case to be the input. Try again with

    use List::Util qw{ shuffle }; my @array = shuffle((undef) x 3, qw/c b a/, ("") x 3);

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Ah, thanks! :)

      > you declared the lucky case to be the input

      In my defence:

      To be sure I also tried

       my @array =  ( ("")x3, (undef)x 3, qw/c b a/, ("")x3, (undef) x3 );

      which still gives for cmp_undef1

      [ "", "", "", "", "", "", "a", "b", "c", undef, undef, undef, undef, undef, undef, ]

      which seems to be another "lucky case".

      I don't know how "stable" is defined but it turns out to be not that easy to construct a counterexample without shuffle.

      my @array =  ( undef, "", qw/c b a/, "", undef  );

      ->

      ["", "a", "b", "c", undef, "", undef]

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re^3: Sort undef
by AnomalousMonk (Bishop) on Jun 12, 2017 at 15:44 UTC

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