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

T

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'.

Thanks in advance!


--
Tore Aursand <[email protected]>

"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."
 
A

Andreas Kahari

Tore Aursand wrote: said:
$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
 
T

Tassilo v. Parseval

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
 
A

Anno Siegel

Tore Aursand said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top