Another idea for you... Maintaining the two tables in sync is a hassle. I was just keying upon I would like to sort @food by putting all non-fruit at the end. Doesn't solve the ranking problem within FRUIT, I just used alpha sort.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use constant FRUIT => 0; use constant VEGGIE =>1; my @food; push @food, { name => "apple", type => FRUIT }; push @food, { name => "banana", type => FRUIT }; push @food, { name => "orange", type => FRUIT }; push @food, { name => "broccoli", type => VEGGIE }; push @food, { name => "grape", type => FRUIT }; my @sorted = sort { $a->{type} <=> $b->{type} or $a->{name} cmp $b->{name} } @food; print Dumper(\@sorted); __END__ $VAR1 = [ { 'name' => 'apple', 'type' => 0 }, { 'type' => 0, 'name' => 'banana' }, { 'type' => 0, 'name' => 'grape' }, { 'name' => 'orange', 'type' => 0 }, { 'type' => 1, 'name' => 'broccoli' } ];
Update: As a practical matter, part of my thinking is that a data structure like this:
my %ranking = ( 1 => 0, 2 => 1, 5 => 2, 10 =>3 );
will be very hard to maintain manually. Here it is possible. But if the tables grow to any size, it won't be feasible and some other structure will be needed to describe the rankings that a human can deal with.

In reply to Re: sort an array of hashes by value by Marshall
in thread sort an array of hashes by value by micmac

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.