The thread $a and strict has discussion of your problem, and Re (tilly) 5: $a and strict has a solution to your problem.

However if your code is not performance critical and you want to have a solution that is more in line with working with objects, you may prefer to create the following base class:

package Sortable; use overload 'cmp' => \&_cmp; sub _cmp { $_[0]->cmp($_[1]); } 1;
Now just inherit from that and define a useful 'cmp' method and you can sort in whatever order makes sense. For instance:
package AnonArray; # Quick demo of how Sortable works use Sortable; @ISA = ('Sortable'); sub cmp { my $self = shift; my $other = shift; my $len = @$self > @$other ? @$self : @$other; for (0..($len - 1)) { if (my $res = $self->[$_] cmp $other->[$_]) { return $res; } } if ($len < @$self) { return 1; } elsif ($len < @$other) { return -1; } else { return 0; } } sub new { my $class = shift; return bless [@_], $class; } 1;
allows you to make anonymous arrays that will compare with other anonymous arrays in a lexicographical sort.

UPDATE
For the record I don't have an opinion on whether the overload approach is a good idea. It was something I was playing with, and it was satisfying to see that I could set up complex sorts using that idea. But I just don't know if it is really worth it to design a system that much at variance with how Perl works by default. If anyone has a more concrete opinion, I would be interested to hear it...

UPDATE 2
Sorry, I should have given an example of what it looks like in action. Well suppose that you create an array of AnonArrays and put it in @to_sort. Then you just try:

@sorted = sort @to_sort;
and the comparison function is picked up behind the scenes from Sortable. :-)

In reply to Re (tilly) 1: OO and sorting by tilly
in thread OO and sorting by artist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.