# Newbie question sorting

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Hi

Could someone please point me to some documentation on how to sort
arrays such as these?

a[0][0]= ANA a[0][1]= 1
a[1][0]= MFI a[1][1]= 12
a[2][0]= ABW a[2][1]= 1
a[3][0]= LTL a[3][1]= 2
a[4][0]= APD a[4][1]= 2

I would like to sort the first column after sorting the second column
numerically.
So the sort of the above data should give back.

ABW 1
ANA 1
APD 2
LTL 2
MFI 12

Thanks

## Re: Newbie question sorting

> Could someone please point me to some documentation on how to sort

perldoc -f sort

> arrays such as these?
>
> a[0][0]= ANA a[0][1]= 1
> a[1][0]= MFI a[1][1]= 12
> a[2][0]= ABW a[2][1]= 1
> a[3][0]= LTL a[3][1]= 2
> a[4][0]= APD a[4][1]= 2

What language is this?

> I would like to sort the first column

How are you defining 'column'?

> after sorting the second column numerically.
> So the sort of the above data should give back.
>
> ABW 1
> ANA 1
> APD 2
> LTL 2
> MFI 12
>

#!/usr/bin/perl
use strict;
use warnings;
my @data = (
['ANA', 1],
['MFI', 12],
['ABW', 1],
['LTL', 2],
['APD', 2],);

print "\$_->[0] \$_->[1]\n" for sort {\$a->[1] <=> \$b->[1] or \$a->[0] cmp
\$b->[0]} @data;

__END__
ABW 1
ANA 1
APD 2
LTL 2
MFI 12

Paul Lalli

## Re: Newbie question sorting

Paul Lalli wrote:
>
> > Could someone please point me to some documentation on how to sort
>
> perldoc -f sort
>
> > arrays such as these?
> >
> > a[0][0]= ANA a[0][1]= 1
> > a[1][0]= MFI a[1][1]= 12
> > a[2][0]= ABW a[2][1]= 1
> > a[3][0]= LTL a[3][1]= 2
> > a[4][0]= APD a[4][1]= 2
>
> What language is this?
>
> > I would like to sort the first column
>
> How are you defining 'column'?
>
> > after sorting the second column numerically.
> > So the sort of the above data should give back.
> >
> > ABW 1
> > ANA 1
> > APD 2
> > LTL 2
> > MFI 12
> >
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> my @data = (
> ['ANA', 1],
> ['MFI', 12],
> ['ABW', 1],
> ['LTL', 2],
> ['APD', 2],);
>
> print "\$_->[0] \$_->[1]\n" for sort {\$a->[1] <=> \$b->[1] or \$a->[0]
cmp
> \$b->[0]} @data;

Can something similar work for a reference to an array of references to
arrays?
I using a "\$data" from a mysql query.

my \$data = \$sth->fetchall_arrayref;

so then the data is actually
\$data->[0][0] = ANA
\$data->[0][1] = 1
\$data->[1][0] = MFI and so forth.

Thanks again for taking the time to help.

Martin Foster.

>
> __END__
> ABW 1
> ANA 1
> APD 2
> LTL 2
> MFI 12
>
>
>
> Paul Lalli

## Re: Newbie question sorting

mdfoster44@netscape.net wrote in news:1108764160.887577.83230

> Can something similar work for a reference to an array of references
> to arrays?
> I using a "\$data" from a mysql query.
>
> my \$data = \$sth->fetchall_arrayref;

Why don't you write the query to return the results in the order you want?

Sinan.

## Re: Newbie question sorting

mdfoster44 wrote:

> Could someone please point me to some documentation on how to sort arrays
> such as these?

Sure: 'perldoc -f sort'.

hth,phay

--
http://www.dunkelheit.at /
thou shallst fear...

## Re: Newbie question sorting

mdfoster44@netscape.net wrote:
> Could someone please point me to some documentation on how to sort
> arrays such as these?
>
> a[0][0]= ANA a[0][1]= 1
> a[1][0]= MFI a[1][1]= 12
> a[2][0]= ABW a[2][1]= 1
> a[3][0]= LTL a[3][1]= 2
> a[4][0]= APD a[4][1]= 2

Which programming language are you dealing with? That does not look like
Perl code to me.

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl