Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sorting an array of hashrefs

Reply
Thread Tools

sorting an array of hashrefs

 
 
John W. Krahn
Guest
Posts: n/a
 
      08-29-2006
monkeys paw wrote:
> The following is intended to sort an array of hash refs based on
> id_type. It is not working however. Does anyone know why?
>
> use Data:umper;
>
> our %datatype_sort = (
> bill => 0,
> reg => 10,
> );
>
>
> my @ary = (
> {id_type => 'reg', id => 'KY200616813'},
> {id_type => 'bill', id => 'CA2005000A5'},
> {id_type => 'bill', id => 'CA2005000A250'},
> );
>
> my @x = sort _sortval_datatype @ary;
>
> print Dumper(\@x);
>
> sub _sortval_datatype {
> my($av, $bv) = @_;
> return $datatype_sort{$av->{id_type}} <=>
> $datatype_sort{$bv->{id_type}};
> }



sub _sortval_datatype {
return $datatype_sort{$a->{id_type}} <=> $datatype_sort{$b->{id_type}};
}



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
monkeys paw
Guest
Posts: n/a
 
      08-29-2006
The following is intended to sort an array of hash refs based on
id_type. It is not working however. Does anyone know why?

use Data:umper;

our %datatype_sort = (
bill => 0,
reg => 10,
);


my @ary = (
{id_type => 'reg', id => 'KY200616813'},
{id_type => 'bill', id => 'CA2005000A5'},
{id_type => 'bill', id => 'CA2005000A250'},
);

my @x = sort _sortval_datatype @ary;

print Dumper(\@x);

sub _sortval_datatype {
my($av, $bv) = @_;
return $datatype_sort{$av->{id_type}} <=>
$datatype_sort{$bv->{id_type}};
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Array objects get changed when sorting the array Roedy Green Java 1 06-25-2009 08:25 PM
Re: Array objects get changed when sorting the array markspace Java 1 06-25-2009 06:22 PM
iterating over a hashref of hashrefs Sam Perl Misc 6 06-15-2005 10:44 AM
grouping hashrefs to trees Matija Papec Perl Misc 1 11-16-2003 04:38 PM
array of hashrefs (nested) monkeys paw Perl Misc 9 10-28-2003 01:55 AM



Advertisments