Newbie question sorting

M

mdfoster44

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
 
G

Gunnar Hjalmarsson

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.
 
P

Paul Lalli

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
 
M

mdfoster44

Paul said:
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;

Thank you for your reply.

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.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top