Just as a remark, you might want to use UNIX time_t format instead of this ASCII-ified version YYYY-MM-DD etc. If you use time_t, you can convert between time zones, and allow for "Daylight Savings Time" settings. If everything is set to GMT, of course, which it should be by default. time_t is what the time function spits out by default.

Then, provided you are doing numeric comparisons, you can sort them with ease. This is super-ultra important, because time_t is rolling over to 10 digits and you don't want to fall victim to The Y2.001775K Bug.

Until September 9, 2001, you can get away with sorting using the default string method. However, any dates after that point will sort back into 1973 territory, which is the bad kind of retro.

So, you can sort them like so:
sub bycreatetime { return $nodes{$a}{createtime} - $nodes{$b}{createtime}; } foreach $node (sort { bycreatetime } keys %nodes) { # ... }
As an added bonus, I see that you're comparing your '$sort_field' variable. Consider the following improvement:
my (%sort_method) = ( createtime => \&bycreatetime, createuser => \&bycreateuser, # For example ); foreach $node (sort $sort_method{$sort_field} keys %nodes) { # ... }
Or, for continued amusement:
sub NumericalSort { my ($field) = @_; return sub { return $nodes{$a}{$field} - $nodes{$a}{$field}; }; } my (%sort_method) = ( createtime => NumericalSort('createtime'), ); foreach $node (sort $sort_method($sort_field) keys %nodes) { # ... }

In reply to Re: Shrink this sort subroutine by tadman
in thread Shrink this sort subroutine by donfreenut

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.