For the sort of sorting (um, sorry) you seem to want to do, treat a {tag} value of 53 as an arbitrary 'high' value and anything else as an arbitrary 'low' value (i.e., just have two) for one level of sorting, and then append to that the sorting you're already doing. To wit:

sub server_sort { # bi-valued key = 0 if {tag} is not 53 and 1 if {tag} is 53 (($my_server_hash{$a}{tag} == 53) <=> ($my_server_hash{$b}{tag} == 53)) || $a cmp $b || $my_server_hash{$a}{tag} <=> $my_server_hash{$b}{tag} } my @server_names = sort &server_sort keys %my_server_hash; # now the server names are sorted with the 53 tags grouped # separately from the rest.

Of course, with a large number of items, this manner of sorting is beau coup inefficient; you may want to precalculate the keys, a la the Schwartzian Transform (or dare I suggest the Advanced Sorting - GRT - Guttman Rosler Transform?).

In this case, the precalculated keys might consist of the various components appended together:

Update: Admittedly, pack would be far more efficient than sprintf in the following, but I wanted to illustrate the point without obfuscating it any more than necessary.

sub make_key { my $sn = shift; my $tag = $my_server_hash{$sn}{tag}; # NOTE: presumes largest server name may be contained # in 20 chars and that {tag} values won't exceed 99,999 return sprintf "%1d%20s%5d", ($tag == 53), $sn, $tag}; } sub decode_key { my $key = shift; return substr $key, 1, 20; } my @server_names = map { decode_key($_) } sort { $a cmp $b } map { make_key($_) } keys %my_server_hash;

Similarly, your other criteria (looks like {tag} is contained within the {ADMINS} key, whatever that means) might be incorporated by changing the bi-valued key to a tri-valued key (0, 1, 2) and inserting that criteria between the two existing ones.

sub make_key { my $sn = shift; my $tag = $my_server_hash{$sn}{tag}; my $member = $my_server_hash{$sn}{ADMINS}; my $key = ($tag == 53) ? 2 : (($member =~ s/$tag/FOUND:$tag/i) && $tag != 1) ? 1 : 0; return sprintf "%1d%20s%5d", $key, $sn, $tag}; }

Hope this helps.

dmm

If you GIVE a man a fish you feed him for a day
But,
TEACH him to fish and you feed him for a lifetime

In reply to Re: Re: hash sort problems by dmmiller2k
in thread hash sort problems by Conal

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.