CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Sorting numerical problem

 



Knight
Deleted

Feb 7, 2001, 7:01 AM

Post #1 of 4 (243 views)
Sorting numerical problem Can't Post

From what I can gather <=> should sort the code in standard numercial format i.e. 1, 4, 10, rather than 1, 10, 4.

Using the following code:




@ODB = map { $_->[0] }

reverse sort {
@a_fields = @$a[1..$#$a];
@b_fields = @$b[1..$#$b];

($a_fields[1] <=> $b_fields[1]) || ($a_fields[2] <=> $b_fields[2]);
}
map { [$_, split /|/] } @ODB;

It does it the wrong way, 1, 10, 4 instead of 1, 4, 10.

Can anyone help?

Thanks



sleuth
Enthusiast

Feb 7, 2001, 11:27 AM

Post #2 of 4 (235 views)
Re: Sorting numerical problem [In reply to] Can't Post

 
sort {$a <=> $b}

Is what you need,

Tony



Knight
Deleted

Feb 8, 2001, 12:47 AM

Post #3 of 4 (226 views)
Re: Sorting numerical problem [In reply to] Can't Post

OK, i nicked this off these forums somewhere:

open (mark,"<$database");
@mark=<mark>;
@mark = map { $_->[0] }
sort { $b->[0] <=> $a->[0]
||
$b->[1] <=> $a->[1]
}
map { [$_, /=(\d+)/, uc($_)] } @mark;
close (mark);

Which sorts the first column, but doesn't then sort the 2nd, any ideas?

Thanks all
Mark



muinatit
Deleted

Feb 8, 2001, 2:04 PM

Post #4 of 4 (219 views)
Re: Sorting numerical problem [In reply to] Can't Post

Change the || to &&

muinatit
muinatit@hotpop.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives