Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sort a hash based on values in the hash stored as arrays of hashes

Reply
Thread Tools

Sort a hash based on values in the hash stored as arrays of hashes

 
 
Tore Aursand
Guest
Posts: n/a
 
      09-16-2003
Hmm. I'm not quite sure if I got the subject right, but I'll try to
explain.

I've got a hash of elements stored like this:

$VAR1 = {
'element_id' => '8',
'elementtype_id' => '3',
'children' => [],
'parents' => [
{
'position' => '5',
'parent_id' => '3'
},
{
'position' => '3',
'parent_id' => '11'
}
]
};

Records like the one above is stored in a hash (called %hierarchy), with
the 'element_id' as key (yeah, I want that value in the hash itself, too).

The problem: Each element can have more than one parent (as illustrated
above), a parent with a position assigned to it. The element above will
exist two places in the hierarchy, as you might figure.

How should I proceed when I want to sort this hash? I want to sort it by
'parent_id', then 'position'.

Thanks in advance!


--
Tore Aursand <>

"You know the world is going crazy when the best rapper is white, the best
golfer is black, France is accusing US of arrogance and Germany doesn't
want to go to war."
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      09-16-2003
In article <>, Tore Aursand wrote:
[cut]
> $VAR1 = {
> 'element_id' => '8',
> 'elementtype_id' => '3',
> 'children' => [],
> 'parents' => [
> {
> 'position' => '5',
> 'parent_id' => '3'
> },
> {
> 'position' => '3',
> 'parent_id' => '11'
> }
> ]
> };

[cut]
> How should I proceed when I want to sort this hash? I want to sort it by
> 'parent_id', then 'position'.


Why do you want to sort it on parent_id? Wouldn't it be the
same thing as sorting it on element_id (if the parent_id's are
the element_id's of the parents)?

Then, if the id's are unique, sorting on position wouldn't do
anything, would it?


Cheers,
Andreas

--
Andreas Kähäri
 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      09-16-2003
Also sprach Tore Aursand:

> Hmm. I'm not quite sure if I got the subject right, but I'll try to
> explain.
>
> I've got a hash of elements stored like this:
>
> $VAR1 = {
> 'element_id' => '8',
> 'elementtype_id' => '3',
> 'children' => [],
> 'parents' => [
> {
> 'position' => '5',
> 'parent_id' => '3'
> },
> {
> 'position' => '3',
> 'parent_id' => '11'
> }
> ]
> };
>
> Records like the one above is stored in a hash (called %hierarchy), with
> the 'element_id' as key (yeah, I want that value in the hash itself, too).
>
> The problem: Each element can have more than one parent (as illustrated
> above), a parent with a position assigned to it. The element above will
> exist two places in the hierarchy, as you might figure.
>
> How should I proceed when I want to sort this hash? I want to sort it by
> 'parent_id', then 'position'.


I don't quite see where 'element_id' comes into the game here. Maybe
this does what you want (lots of curlies ahead):

@{ $hierarchy{parents} } = sort {
$a->{parent_id} <=> $b->{parent_id}
or
$a->{position} <=> $b->{position}
} @{ $hierarchy{parents} }

The "$a->{position} <=> $b->{position}" is only taken into account when
the comparison of the parent_id resulted in 0 (which happens when they
are equal).

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-16-2003
Tore Aursand <> wrote in comp.lang.perl.misc:
> Hmm. I'm not quite sure if I got the subject right, but I'll try to
> explain.
>
> I've got a hash of elements stored like this:
>
> $VAR1 = {
> 'element_id' => '8',
> 'elementtype_id' => '3',
> 'children' => [],
> 'parents' => [
> {
> 'position' => '5',
> 'parent_id' => '3'
> },
> {
> 'position' => '3',
> 'parent_id' => '11'
> }
> ]
> };
>
> Records like the one above is stored in a hash (called %hierarchy), with
> the 'element_id' as key (yeah, I want that value in the hash itself, too).
>
> The problem: Each element can have more than one parent (as illustrated
> above), a parent with a position assigned to it. The element above will
> exist two places in the hierarchy, as you might figure.


I don't follow. If the elements are stored under their element_id, there
*can* only be one element with id 8 in the hash. How can it exist in two
places?

> How should I proceed when I want to sort this hash? I want to sort it by
> 'parent_id', then 'position'.


Since an element can have multiple parents, which parent-id and position
are you going to sort by? Can an element also have no parents?

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash whose values are arrays of hashes davidmichaelkarr@gmail.com Perl Misc 7 06-01-2006 04:54 PM
How does one sort hash of hashes by multiple values? joe.yakich@gmail.com Ruby 3 11-05-2005 02:51 AM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of hashes or just one hash ? Perl Learner Perl Misc 11 06-09-2005 06:55 AM
Performance Improvement of complex data structure (hash of hashes of hashes) Scott Gilpin Perl Misc 2 08-26-2004 01:02 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57