sub sorthasharray($$) { # takes reference to array of hashes, and arra +y of key names to sort by. my ($hasharray, $keylist) = @_; my $keyCount; # counter my $hashCount; # counter for $hashCount ( 0 .. $#$hasharray ) { # walk through the arr +ay for $keyCount ( 0 .. $#$keylist ) { # walk through the + keys if ( $$hasharray[$hashCount]{$$keylist[$keyCou +nt]} =~ /^\d*?\.??\d*?$/ ) { # check to see if this should be a nume +ric comparison my $zeros = 12 - (length($$hasharray[$ +hashCount]{$$keylist[$keyCount]})); # find out how many zeros we nee +d #to pad to the right to make a string comparison equivalent to a numer +ic comparison #(this isn't onehundred percent accurate, especially for float numbers +. Anyone want to take a crack at it? for $zeroCount ( 1 .. $zeros ) { $$hasharray[$hashCount]{sortke +y} .= "0"; # padd the zeros } } $$hasharray[$hashCount]{sortkey} .= $$hasharra +y[$hashCount]{$$keylist[$keyCount]}; # build sortkey by concatenatin +g all sort keys in order } } @$hasharray = sort { $$a{sortkey} cmp $$b{sortkey} } @$hasharr +ay; # do the sort for $hashCount ( 0 .. @$hasharray ) { delete $$hasharray[$hashCount]{sortkey}; # delete the + unneeded key from all the hashes } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Sort Array of Hashes by values of multiple hash keys.
by lhoward (Vicar) on Jun 19, 2000 at 19:38 UTC | |
by davorg (Chancellor) on Jun 21, 2000 at 13:04 UTC | |
by raflach (Pilgrim) on Jun 19, 2000 at 19:49 UTC | |
|
RE: Sort Array of Hashes by values of multiple hash keys.
by BBQ (Curate) on Jun 20, 2000 at 08:16 UTC |