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

Home: Perl Programming Help: Beginner:
Sorting numerical problem

Knight
Deleted

Feb 7, 2001, 7:01 AM

Post #1 of 4 (316 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 (308 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 (299 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 (292 views)
 Re: Sorting numerical problem [In reply to] Can't Post
Change the || to &&

muinatit
muinatit@hotpop.com

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0