Perl sorting problem

sssolis
sssolis used Ask the Experts™
on
I have a muti-dimensional array in perl that I am trying to sort by one of the columns this way:

#here I populate the array with the database records
$i=0;
while (@field = $cursor->fetchrow){    
     $nfields[0][$i] = $field[0]; #categoryid
     $nfields[1][$i] = $field[1]; #q_id
     $nfields[2][$i] = $field[2]; #q_type
     $nfields[3][$i] = $field[3]; #q_level    
        $nfields[4][$m] = $field[4]; #q_cat    
$i++;
}
#then I sort it by the 4 column which is alfabetic
@nsorted = sort{$a->[4] cmp $b->[4]} @nfields;

when I try printing the sorted array, it prints the same as before . The 4th column containts titles like "chapter 01" "Chapter 02" etc that should be soreted asc.

thanks

Saul

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2014
Top Expert 2015
Commented:
#perhaps you meant
while (@field = $cursor->fetchrow){    
    $nfields[$i][0] = $field[0]; #categoryid
    $nfields[$i][1] = $field[1]; #q_id
    $nfields[$i][2] = $field[2]; #q_type
    $nfields[$i][3] = $field[3]; #q_level    
    $nfields[$i][4] = $field[4]; #q_cat    
    $i++
}
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
#if that is what you meant, it could be done more easily as
while( @field = $cursor->fetchrow ){    
    push @nfields,[@field];
}

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial