Hi monks, i'm looking for a way to do sorts in a non-numerical and numerical way...
this is the code i've got so far but it doesnt handle non-numerical items.
#!/usr/bin/perl use strict; use warnings; my %item1 = ( "item" => 1, "name" => "smith, john", "age" => 25, "height" => 1.80 ); my %item2 = ( "item" => 2, "name" => "jones, bob", "age" => 26, "height" => 1.70 ); my %item3 = ( "item" => 3, "name" => "arrang, eliee", "age" => 27, "height" => 1.75 ); my %hash; $hash{1} = { %item1 }; $hash{2} = { %item2 }; $hash{3} = { %item3 }; my $orderby = "name"; foreach my $id (sort { $hash{$b}{$orderby} <=> $hash{$a}{$orderby} } k +eys %hash) { print "$hash{$id}{'item'} - $hash{$id}{$orderby}\n"; } # end-foreach exit;
Can anyone help me out?

Cheers,
Reagen

In reply to numerical and non numerical sorts by rsiedl

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.