in reply to Re: Re: Re: Sort problem
in thread Sort problem

The example I posted uses concatenation but removes the spaces \040 and thus the problem.

@in = ( ['the cat', 'sat', 'on', 'the', 'mat'], ['the cat sat', 'wherever if felt like'], + ['the cat'], ); my @out = map{$_->[0]} sort{$a->[1] cmp $b->[1]} map{[$_, concat($_ +)]} @in; sub concat { my $ary = shift; $ary = join '', @$ary; $ary =~ s/\s//g; return $ary; } use Data::Dumper; $Data::Dumper::Indent = 0; print map {s/\[/\n \[/g; $_} Dumper \@out; __DATA__ $VAR1 = [ ['the cat'], ['the cat','sat','on','the','mat'], ['the cat sat','wherever if felt like']];

Please explain to me why this is not appropriately sorted and how it is failing? It seemed clear enough to me that he did not want 'the cat' to sort before ('the', 'cat') simply because char 4 of 'the cat' is \040. If I misread the problem and he wants to sort including embeded spaces but not getting a space when you concat with "@ary" then all is required is a local $" = ''; before the sort.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Sort problem
by jsprat (Curate) on Feb 26, 2003 at 20:26 UTC
    Try it with this sample data, for example:

    @in = ( ['the cat sat'], ['the ', 'cat', 'sat'], ['the cat', 'sat'], ['the', 'cat', 'sat'], );

    I would expect it to be sorted 4, 2, 3, 1. Concatenating and compressing the spaces, then using the ST returns the array in its original order.

      If that is the problem (hard to tell from the post) then all you need is $" = '';

      @in = ( ['the cat sat'], ['the ', 'cat', 'sat'], ['the cat', 'sat'], ['the', 'cat', 'sat'], ); { local $" = ''; @out = sort { "@$b" cmp "@$a" } @in; } use Data::Dumper; # munge the output for compactness and to make it look nicer $Data::Dumper::Indent = 0; print map {s/\[/\n \[/g; $_} Dumper \@out; __DATA__ $VAR1 = [ ['the','cat','sat'], ['the ','cat','sat'], ['the cat','sat'], ['the cat sat']];

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Re: Re: Re: Re: Sort problem
by dws (Chancellor) on Feb 26, 2003 at 20:35 UTC
    The example I posted uses concatenation but removes the spaces \040 and thus the problem. ... Please explain to me why this is not appropriately sorted ...

    To borrow from the standard example of why naive hyphenation can cause problems, if you remove spaces,   [qw(therapist)] sorts the same as   [qw(the rapist)]

      Given the tabloid stories you're telling me there is a difference!

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print