sorting array in multiple columns

K

kenlo888

Hi,

I have data in an array like:

(1,'black'), (2,'black'), (1,'orange'), (2,'brown'), (1,'green')

I want to sort the first column, then the second column. Very similar
to a sql order by statement "order by row1, row2".

The desired output would be:

(1,'black'), (1,'green'), (1,'orange'), (2,'black),(2,'brown')

I understand how to use the sort function in perl...

@sorted_Array=sort{$a->[$sort_row]cmp$b->[$sort_row]}@array;

but this can only sort one set of rows. If you have any examples of
multiple row sorts, that would be great.

Thanks.

-Ken
 
J

John Bokma

Hi,

I have data in an array like:

(1,'black'), (2,'black'), (1,'orange'), (2,'brown'), (1,'green')

I want to sort the first column, then the second column. Very similar
to a sql order by statement "order by row1, row2".

The desired output would be:

(1,'black'), (1,'green'), (1,'orange'), (2,'black),(2,'brown')


That's not Perl
I understand how to use the sort function in perl...

@sorted_Array=sort{$a->[$sort_row]cmp$b->[$sort_row]}@array;

but this can only sort one set of rows. If you have any examples of
multiple row sorts, that would be great.


perldoc -f sort
 
T

Tad McClellan

I have data in an array like:

(1,'black'), (2,'black'), (1,'orange'), (2,'brown'), (1,'green')


OK, if you say so...


-------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my @array = (1,'black'), (2,'black'), (1,'orange'), (2,'brown'), (1,'green');
print "unsorted:\n";
print "$_\n" for @array;

print "\n";
print "there are ", scalar(@array), " elements in \@array\n";
print "\n";

my @sorted = sort @array;
print "sorted:\n";
print "$_\n" for @sorted;
-------------------------------

outputs:
Useless use of a constant in void context at ./temp line 5.
Useless use of a constant in void context at ./temp line 5.
Useless use of a constant in void context at ./temp line 5.
Useless use of a constant in void context at ./temp line 5.
Useless use of a constant in void context at ./temp line 5.
Useless use of a constant in void context at ./temp line 5.
unsorted:
1
black

there are 2 elements in @array

sorted:
1
black

I want to sort the first column, then the second column.


There are only 2 elements in your array, and they are already
in sorted order.

Have you seen the Posting Guidelines that are posted here frequently?

@sorted_Array=sort{$a->[$sort_row]cmp$b->[$sort_row]}@array;


Whitespace is not a scarce resource, feel free to use as much of it
as you like to make your code easier to read and understand.

@sorted_Array = sort { $a->[$sort_row] cmp $b->[$sort_row] } @array;

If you have any examples of
multiple row sorts, that would be great.


Everybody, including you, already have examples of multiple row sorts
on their own hard disks.

perldoc -q sort

Please check the Perl FAQ *before* posting to the Perl newsgroup!
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top