sorting array reference of hashes

M

monkeys paw

I have an array of hashes like so:

$f = [ {
'file_name' => 'TakeTwo',
'id_type' => 'bill',
'id' => 'CA2005010A1',
},
{
'file_name' => 'steve',
'id_type' => 'bill',
'id' => 'CA2005000A3',
},
];

I want to sort by file_name.

I am trying this:

sort sort_by_filename @{$f};

sub sort_by_filename {

return $a->{file_name} cmp $b->{file_name}

}

This does not work. How would one do this??
 
U

Uri Guttman

mp> I have an array of hashes like so:
mp> $f = [ {
mp> 'file_name' => 'TakeTwo',
mp> 'id_type' => 'bill',
mp> 'id' => 'CA2005010A1',
mp> },
mp> {
mp> 'file_name' => 'steve',
mp> 'id_type' => 'bill',
mp> 'id' => 'CA2005000A3',
mp> },
mp> ];

mp> I want to sort by file_name.

mp> I am trying this:

mp> sort sort_by_filename @{$f};

mp> sub sort_by_filename {

mp> return $a->{file_name} cmp $b->{file_name}

mp> }

mp> This does not work. How would one do this??

never use the term "doesn't work" without explaining what you
expected. what kind of results did you get that you didn't like? were
those the only 2 records to be sorted?

now, i feel i know the answer to your problem as i bet 'TakeTwo' sorted
before steve. have you ever done ascii order sorts? ever heard of case
issues? you need to make the sort sub ignore case and i leave that as an
exercise to the OP.

uri
 
A

anno4000

monkeys paw said:
I have an array of hashes like so:

$f = [ {
'file_name' => 'TakeTwo',
'id_type' => 'bill',
'id' => 'CA2005010A1',
},
{
'file_name' => 'steve',
'id_type' => 'bill',
'id' => 'CA2005000A3',
},
];

I want to sort by file_name.

I am trying this:

sort sort_by_filename @{$f};

sub sort_by_filename {

return $a->{file_name} cmp $b->{file_name}

}

This does not work. How would one do this??

Your example is unfortunate because it already is in sorted order.
That raises the question how you know it doesn't work.

More generally, what did you expect it to do? Sort the array in place?
That isn't what sort() does, it returns the sorted list. To emulate
in-place sorting, you can do

@$f = sort sort_by_filename @$f;

Anno
 
T

Tad McClellan

monkeys paw said:
sort sort_by_filename @{$f};


You should always enable warnings when developing Perl code!

This does not work.


What did you observe that led you to conclude that it is not working?

Your program has no output statements in it, so the program is
_supposed_ to make no output.

How would one do this??


my @sorted = sort sort_by_filename @{$f};

print "$_->{file_name}\n" for @sorted;
 
M

monkeys paw

Sorry everyone, i did some more on this problem and it is working.
I simplified my example too much for proper explanation.
My answer was:
$f = [ {
'file_name' => 'TakeTwo',
'id_type' => 'bill',
'id' => 'CA2005010A1',
},
{
'file_name' => 'steve',
'id_type' => 'bill',
'id' => 'CA2005000A3',
},
];

@ordered = sort {lc($a->{file_name}) cmp lc($b->{file_name})} @{$f};

monkeys said:
I have an array of hashes like so:

$f = [ {
'file_name' => 'TakeTwo',
'id_type' => 'bill',
'id' => 'CA2005010A1',
},
{
'file_name' => 'steve',
'id_type' => 'bill',
'id' => 'CA2005000A3',
},
];

I want to sort by file_name.

I am trying this:

sort sort_by_filename @{$f};

sub sort_by_filename {

return $a->{file_name} cmp $b->{file_name}

}

This does not work. How would one do this??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top