I'd create a hash of indexes. The key is the property and the value is an array of primary keys in your main hash, ordered by the property in question. It's very fast and reasonable straight forward.

Most of the following code is creating the test data and then displaying the results. It's just the middle of the 3 blocks that does all the work setting up the indexes.

#! perl -slw use strict; use Data::Dumper; use List::Util qw[ shuffle ]; ## An array of properties my @props = map{ "prop$_" } 1 .. 10; ## Gerate some test data my %things = map { +"thing$_" => { map{ $_ => int rand 1000 } ( shuffle @props )[ 0 .. 4 ] } } '0001' .. '3000'; ## Create a hash of indexes sorted by the the values of the properties my %orderings = map{ my $prop = $_; $prop => [ sort { $things{ $a }{ $_ } <=> $things{ $b }{ $_ } } map{ exists $things{ $_ }{ $prop } ? $_ : () } keys %things ] } @props; ## Display everything, in every order for my $prop ( @props ) { print "\nThings ordered by $prop\n"; for my $thing ( @{ $orderings{ $prop } } ) { print $thing, ' => { ', join( '', map{ sprintf "%6.6s: %3.3s, ", exists $things{ $thing }{ $_ } ? ( $_, $things{ $thing }{ $_ } ) : ( '', '' ) } @props ) . ' }'; } }

Demo run with to 50 items and 2/4 properties to keep the volume of output down but it handles 10,000 items and 10 properties near instantly.

[20:37:42.48] P:\test>422357 Things ordered by prop1 thing0016 => { prop1: 6, prop2: 570, : , : , } thing0047 => { prop1: 11, : , : , prop4: 77, } thing0040 => { prop1: 40, : , prop3: 542, : , } thing0029 => { prop1: 66, prop2: 346, : , : , } thing0013 => { prop1: 124, : , prop3: 709, : , } thing0020 => { prop1: 136, : , prop3: 961, : , } thing0046 => { prop1: 141, prop2: 904, : , : , } thing0048 => { prop1: 205, : , : , prop4: 292, } thing0027 => { prop1: 206, : , : , prop4: 180, } thing0006 => { prop1: 212, prop2: 695, : , : , } thing0041 => { prop1: 230, prop2: 942, : , : , } thing0030 => { prop1: 276, : , : , prop4: 505, } thing0010 => { prop1: 300, prop2: 980, : , : , } thing0021 => { prop1: 314, : , : , prop4: 38, } thing0034 => { prop1: 340, : , prop3: 637, : , } thing0002 => { prop1: 365, : , : , prop4: 180, } thing0012 => { prop1: 491, prop2: 884, : , : , } thing0023 => { prop1: 527, : , : , prop4: 813, } thing0035 => { prop1: 622, : , : , prop4: 0, } thing0049 => { prop1: 701, : , : , prop4: 311, } thing0003 => { prop1: 704, : , prop3: 553, : , } thing0009 => { prop1: 715, : , : , prop4: 939, } thing0028 => { prop1: 779, : , prop3: 405, : , } thing0032 => { prop1: 787, prop2: 545, : , : , } thing0019 => { prop1: 830, : , prop3: 683, : , } thing0024 => { prop1: 842, prop2: 668, : , : , } thing0033 => { prop1: 868, : , : , prop4: 413, } thing0011 => { prop1: 898, : , : , prop4: 68, } thing0038 => { prop1: 908, : , : , prop4: 218, } thing0045 => { prop1: 966, : , prop3: 4, : , } Things ordered by prop2 thing0039 => { : , prop2: 102, : , prop4: 178, } thing0005 => { : , prop2: 269, prop3: 514, : , } thing0036 => { : , prop2: 283, prop3: 512, : , } thing0037 => { : , prop2: 304, prop3: 636, : , } thing0029 => { prop1: 66, prop2: 346, : , : , } thing0017 => { : , prop2: 429, : , prop4: 426, } thing0032 => { prop1: 787, prop2: 545, : , : , } thing0026 => { : , prop2: 554, prop3: 363, : , } thing0016 => { prop1: 6, prop2: 570, : , : , } thing0050 => { : , prop2: 582, prop3: 484, : , } thing0004 => { : , prop2: 642, prop3: 41, : , } thing0024 => { prop1: 842, prop2: 668, : , : , } thing0006 => { prop1: 212, prop2: 695, : , : , } thing0044 => { : , prop2: 837, prop3: 324, : , } thing0042 => { : , prop2: 842, prop3: 732, : , } thing0012 => { prop1: 491, prop2: 884, : , : , } thing0046 => { prop1: 141, prop2: 904, : , : , } thing0007 => { : , prop2: 925, : , prop4: 938, } thing0041 => { prop1: 230, prop2: 942, : , : , } thing0014 => { : , prop2: 950, : , prop4: 964, } thing0010 => { prop1: 300, prop2: 980, : , : , } thing0018 => { : , prop2: 984, prop3: 561, : , } Things ordered by prop3 thing0045 => { prop1: 966, : , prop3: 4, : , } thing0008 => { : , : , prop3: 6, prop4: 433, } thing0004 => { : , prop2: 642, prop3: 41, : , } thing0031 => { : , : , prop3: 41, prop4: 970, } thing0043 => { : , : , prop3: 196, prop4: 849, } thing0044 => { : , prop2: 837, prop3: 324, : , } thing0026 => { : , prop2: 554, prop3: 363, : , } thing0028 => { prop1: 779, : , prop3: 405, : , } thing0025 => { : , : , prop3: 455, prop4: 578, } thing0050 => { : , prop2: 582, prop3: 484, : , } thing0036 => { : , prop2: 283, prop3: 512, : , } thing0005 => { : , prop2: 269, prop3: 514, : , } thing0040 => { prop1: 40, : , prop3: 542, : , } thing0003 => { prop1: 704, : , prop3: 553, : , } thing0018 => { : , prop2: 984, prop3: 561, : , } thing0001 => { : , : , prop3: 570, prop4: 994, } thing0037 => { : , prop2: 304, prop3: 636, : , } thing0034 => { prop1: 340, : , prop3: 637, : , } thing0019 => { prop1: 830, : , prop3: 683, : , } thing0013 => { prop1: 124, : , prop3: 709, : , } thing0042 => { : , prop2: 842, prop3: 732, : , } thing0022 => { : , : , prop3: 733, prop4: 831, } thing0015 => { : , : , prop3: 921, prop4: 822, } thing0020 => { prop1: 136, : , prop3: 961, : , } Things ordered by prop4 thing0035 => { prop1: 622, : , : , prop4: 0, } thing0021 => { prop1: 314, : , : , prop4: 38, } thing0011 => { prop1: 898, : , : , prop4: 68, } thing0047 => { prop1: 11, : , : , prop4: 77, } thing0039 => { : , prop2: 102, : , prop4: 178, } thing0027 => { prop1: 206, : , : , prop4: 180, } thing0002 => { prop1: 365, : , : , prop4: 180, } thing0038 => { prop1: 908, : , : , prop4: 218, } thing0048 => { prop1: 205, : , : , prop4: 292, } thing0049 => { prop1: 701, : , : , prop4: 311, } thing0033 => { prop1: 868, : , : , prop4: 413, } thing0017 => { : , prop2: 429, : , prop4: 426, } thing0008 => { : , : , prop3: 6, prop4: 433, } thing0030 => { prop1: 276, : , : , prop4: 505, } thing0025 => { : , : , prop3: 455, prop4: 578, } thing0023 => { prop1: 527, : , : , prop4: 813, } thing0015 => { : , : , prop3: 921, prop4: 822, } thing0022 => { : , : , prop3: 733, prop4: 831, } thing0043 => { : , : , prop3: 196, prop4: 849, } thing0007 => { : , prop2: 925, : , prop4: 938, } thing0009 => { prop1: 715, : , : , prop4: 939, } thing0014 => { : , prop2: 950, : , prop4: 964, } thing0031 => { : , : , prop3: 41, prop4: 970, } thing0001 => { : , : , prop3: 570, prop4: 994, }

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

In reply to Re: Choosing "best" - sorting hashes issue by BrowserUk
in thread Choosing "best" - sorting hashes issue by Ineffectual

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.