Gossamer Forum
Home : General : Perl Programming :

Re-sort an Array of Hashes (LinksSQL)

Quote Reply
Re-sort an Array of Hashes (LinksSQL)

I've brought this over from the LSQL Discussion forum because I think some of you guys could possibly help (its more Perl related than LSQL I think?)

I'm working on a fix for displaying related subcategories in the correct order when LSQL builds the subcategory list.

The list of subcats and related subcats is put into an array of hashes using ID's to get the name. The 'Name' for related subcats is that of its linked parent, rather than the 'Related Name' - the related name is applied later, and that's why the order is not correct.

To solve this problem I want to re-sort the array of hashes afterwards, which by then includes the proper related name in the field 'Name'.

Here's what I came up with after looking around the web for help:

my $sort_data = $display{category_loop};
foreach my $hashref ( sort {$$a{Name} cmp $$b{Name}} @{$sort_data}) {
if ($hashref->{Name} eq $display{Name}) {
else {
push @{$display{category_sorted_loop}}, $hashref;

The if removes a category link to itself that seems to appear after the sort (not sure why that happens)?

Maybe I haven't quite got the sort statement right? (I'm trying to re-sort the array of hashes based on the 'Name' value in each hash)

Anyway, it works fairly well, but it chops the first category name. The first one is present in $display{category_loop}, but it disappears after the sort and is not there in $display{category_sorted_loop}.

Any ideas what is going wrong?

If you want to see the value of the arrays, have a look here (page down):

Sorry for the duplicate post, but I'm keen to get this working properly.

It's 90% there, but it does insist on removing the first subcat?

Any help greatly appreciated.

All the best
Quote Reply
Re: [qango] Re-sort an Array of Hashes (LinksSQL) In reply to
I took another look at this and I think I've got it now - apologies for duplicate thread.

All the best