Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^3: Custom Sort An AoA

by SuicideJunkie (Vicar)
on Apr 01, 2014 at 17:43 UTC ( #1080609=note: print w/replies, xml ) Need Help??

in reply to Re^2: Custom Sort An AoA
in thread Custom Sort An AoA

If all the data were numeric and limited in length, you could zero-pad it before the join and then CMP it since <=> will probably explode on longer arrays.

If you need it to be fully generic, you could always break down and make the sort block be a complex sub that loops over the elements and returns once it finds a difference.

sub sortIt { my $result = @$a <=> @$b; my $idx = $#$a; while (!$result && $idx >=0) { $result = $a->[$idx] cmp $b->[$idx]; $idx--; } return $result; }

Replies are listed 'Best First'.
Re^4: Custom Sort An AoA
by LanX (Bishop) on Apr 01, 2014 at 18:42 UTC
    I'd do it too with an extra helper function, but I would keep the first condition out (for readability, flexibility and reuse)

    something like

    sub cmp_vec { # return either -1,0 or 1 comparing over two equally sized arrays } sort { @$a <=> @$b or cmp_vec($a,$b) } @list;

    for full flexibility a cmp-function could be passed as call-back in third position.

    As a side note, I'm not sure if this could be done with Perl6 Meta ops, something like @a >>cmp<< @b (after reversing of course).

    Cheers Rolf

    ( addicted to the Perl Programming Language)


    something like

    sub cmp_vec { my ($a,$b) = @_; for (reverse 0.. @$a-1) { if (my $x= $a->[$_] cmp $b->[$_]) { return $x } } return 0; ]
      You may be amused to know that this question is being used to help me identify and organize execution paths through 6502 Assembly. (See also my response to shmem).

      Cheers - L~R

Re^4: Custom Sort An AoA
by Limbic~Region (Chancellor) on Apr 01, 2014 at 17:54 UTC
    ...and limited in length....

    You would also need to know that limit and pad everything at every level or else it would still be very complex.

    If you need it to be fully generic...

    Ah yes, this is along the lines of what I was looking for.

    Cheers - L~R

      Ah yes, this is along the lines of what I was looking for.

      Although it feels a bit strange spoon-feeding you a solution... ;)

      #!/usr/bin/perl -l sub LRsort { @$a <=> @$b || do { for (1..@$a) { my $LR = $a->[-$_] =~ /^\d+(?:\.\d+)?$/ && $b->[-$_] =~ /^\d+(?:\.\d+)?$/ ? $a->[-$_] <=> $b->[-$_] : $a->[-$_] cmp $b->[-$_]; return $LR if $LR; } } } sub d { print '[',join(', ',map{"'$_'"}@$_),']' for @_ } my @list = ( ['blah', 'asdf', 'foo', 'bar'], ['two'], ['zzz', 'def', 'ghi'], ['one'], ['mmm', 'def', 'ghi'], ['qqq', 'xyz', 'aaa'], ); my @sorted = sort LRsort @list; print "strings sorted"; d @sorted; $_ = [ map { my $o; $o+= ord for split//;$o } @$_ ] for @list; print "strings numified"; d @list; my @sorted = sort LRsort @list; print "numeric"; d @sorted; _END_ strings sorted ['one'] ['two'] ['qqq', 'xyz', 'aaa'] ['mmm', 'def', 'ghi'] ['zzz', 'def', 'ghi'] ['blah', 'asdf', 'foo', 'bar'] strings numified ['407', '414', '324', '309'] ['346'] ['366', '303', '312'] ['322'] ['327', '303', '312'] ['339', '363', '291'] numeric ['322'] ['346'] ['339', '363', '291'] ['327', '303', '312'] ['366', '303', '312'] ['407', '414', '324', '309']

      Implementing the orcish maneuver as kennethk suggested is left as an excercise for the reader.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        I almost started the thread with I know this is false laziness but.... You are right that this was well within my capabilities. I am not one to make excuses but I have been completely immersed in 6502 Assembly since last Friday afternoon. In fact, this code was to identify an execution path through some assembly. When the answer didn't immediately pop into my head I decided it was best not to think about it. I knew if I switched contexts from Assembly (which I am just learning) to Perl, the effort to regain where I was would be a lot of work.

        Mea culpa

        Cheers - L~R

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1080609]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-06-25 01:06 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.