I am facing an issue with the sort functionality on my Application. I need to sort my Hash of hashes on the lname key under the instructors. The legacy
application is written in Perl.
Here is the dump of the Hash which i need to sort.
$VAR1 = {
'instructors' => [
{
'is_placeholder' => 0,
'lname' => 'Lordy',
'name' => 'Daniel Lordy'
},
{
'is_placeholder' => 0,
'lname' => 'Fisher',
'name' => 'Bethy Fisher'
},
{
'is_placeholder' => 0,
'lname' => 'Jaya',
'name' => 'Jennifer Jaya'
},
],
'id' => '1237058',
'XXX' => {
'name' => 'Fall 2015 MFT Master 695',
},
'YYY' => '45'
};
The instructors key in the above structure can be empty as well. For Example:
$VAR1 = {
'instructors' => [],
'id' => '1237058',
'XXX' => {
'name' => 'Fall 2015 MFT Master 695',
},
'YYY' => '45'
};
In my application, Users have an option to sort the column based on instructor names. So when user sorts by ascending order, the application should show rows which have instructors are empty at the start and then show the rest of the rows in which each row has the instructor names sorted in ascending order. Vice versa for Descending Order.
This is the code which I have tried until now.
if( $sort_order eq 'ASC' ) {
foreach my $elem ( @$course_sections ) {
my @sorted = map { $_->[1] }
sort { $a->[0] cmp $b->[0] }
map { [$_->{'lname'}, $_] } @{$elem->{'instructors'}};
}
if( $sort_order eq 'DESC' ) {
foreach my $elem ( @$course_sections ) {
my @sorted = map { $_->[1] }
sort { $b->[0] cmp $a->[0] }
map { [$_->{'lname'}, $_] } @{$elem->{'instructors'}};
}
How do I get this @sorted hash affect the order of rows in @$course_sections. Let me know if there is any easier way to do it.
Thanks in Advance.
You need to replace each
instructorsarray ref with the sorted version that you created in yourforeachloop. That way you get the instructors of each individual row sorted. Then you can sort the whole$course_sectionsby the name of the first instructor of each row.Make sure to replace
undefvalues with empty strings so thecmpdoesn't blow up. We shouldn't do$_->{'instructors'}->[0]->{'lname'} // q{}because autovivification might create a bunch of empty stuff in our data structure.Here's your example data pulled together:
And this is the output, dumped with Data::Printer.