Hello All,
I want to sort the values of anonymous hashes contained in an array, but the values of each hash contain either strings OR numbers. So I have a data structure like:
@data = (
{
name => "bob",
age => 18,
},
{
name => "jim",
age => 24,
}
);
Obviously if I sort on "name", they are strings and if I sort on "age" they are numbers. The best solution I've come up with so far is:
my $field = "name"; ## or $field = "age";
@sorted = sort
{
if ($a->{$field} =~ /
/){
$a->{$field} cmp $b->{$field}
}else{
$a->{$field} <=> $b->{$field}
}
} @data;
This works, but I'm wondering if there is a less ugly way to do it.
Thanks.