Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

given:
foreach my $obj (@ArrayofObjects){ $obj->print }
How do I sort that to print in price order? I tried something like this:
foreach my $obj (sort @ArObj[$a]->price <=> @ArObj[$b]->price} @ArObj) + { # stuff here ... }
but does not work...What am I missing? Cheers...

Replies are listed 'Best First'.
Re: how to sort an array of (Moose) objects
by Corion (Patriarch) on Apr 13, 2018 at 16:57 UTC

    How does it fail?

    Note that your sort routine as written above does not get array indices but the objects in $a and $b. The syntax for sort is also different. Maybe you want:

    foreach my $obj (sort { $a->price <=> $b->price } @ArObj) { ... }
      Yippe! You nailed it - that was it - (Christ I will never understand Perl sorts...) Thanks man!