Sort magic

Nov 9, 09:00 PM

Fuck this took me ages to figure out with all the reference brainfuck.

Here’s the story:

I have a hash of anonymous arrays, and I want to sort the hash by the elements at index 2. Sort of like when you sort your mails not by date, but by subject or something like that.

Basically I need to create a sorted list of the elements at index 2 of the hash:

@sorted_keys = sort( 
    { ${$hash{$b}}[2] <=> ${$hash{$a}}[2] } keys %hash 
);

This will dereference every value (every array) in %hash and take its 3rd element (index 2), then sort the keys ($a and $b) by it and store that list in @sorted_keys.

Damn!

Just so this solution doesn’t get lost and others may benefit from it, I’m posting it here.

Malte

,

---

say something

Commenting is closed for this article.