I'm trying to sort the following data structure in Perl, by location_id.
my $employees = $dbh->selectall_arrayref(qq[
SELECT name, type, code, emp_cat_id,
percentage, location_id
FROM table_1
],{ Slice => {} });
for my $row (@$employees) {
push @{
$args->{employees}{ $row->{emp_cat_id} }
}, $row;
}
Example:
123 => [
{
percentage => 0.25,
code => "XYZ",
name => "John Doe",
type => "pt",
location_id => 001,
emp_cat_id => 123
}
],
555 => [
{
percentage => 0.50,
code => "ZZZ"
name => "Chris Cringle",
type => "ft",
location_id => 007,
emp_cat_id => 555
},
{
percentage => 0.25,
code => "XXX"
name => "Tom Thompson",
type => "pt",
location_id => 002,
emp_cat_id => 555
}
]
For every emp_cat_id, I need the structure to have the location_ids in asc order.
I've tried the following, but I get "useless use of sort in void context at line #" or "useless use of sort in scalar context at line #" errors.
$args->{employees} = sort {
$a->{location_id} <=> $b->{location_id}
} $args->{employees};
Any help understanding the sort is appreciated!
So, you have a hashref where each element is an arrayref of hashrefs that should be sorted based on a key of that inside hashref?
The important part you're missing is in dereferencing the arrayrefs.
@$arrayref
instead of just$arrayref
.