# sort from the smallest number to the highest number - PERL Beginners

Hi Just say I want to sort the row by using the fifth column data as reference from the smallest the largest number, if using sort, It will give me result like this Abc 12.8 8 "left" 1 1.7 Def 13.8 9 "top" 0 19.7 gef 14.8 9 "left" 0 19.7 Dgf 12.3 9 "right" 4 2.6 bac 12.8 8 "left" 1 3.7 etg 12.8 2 "left" 7 34.7 efg 16.8 5 "right" 0 56.7 baf 32.8 7 "bottom" 5 79.8 cef 16.8 4 "right" 0 89.7 but what I want is like this, what so I do to achieve ...

1. ## sort from the smallest number to the highest number

Hi
Just say I want to sort the row by using the fifth column data as reference from the smallest the largest number, if using sort,
It will give me result like this

Abc 12.8 8 "left" 1 1.7
Def 13.8 9 "top" 0 19.7
gef 14.8 9 "left" 0 19.7
Dgf 12.3 9 "right" 4 2.6
bac 12.8 8 "left" 1 3.7
etg 12.8 2 "left" 7 34.7
efg 16.8 5 "right" 0 56.7
baf 32.8 7 "bottom" 5 79.8
cef 16.8 4 "right" 0 89.7

but what I want is like this, what so I do to achieve this effect?

Abc 12.8 8 "left" 1 1.7
Dgf 12.3 9 "right" 4 2.6
bac 12.8 8 "left" 1 3.7
Def 13.8 9 "top" 0 19.7
gef 14.8 9 "left" 0 19.7
etg 12.8 2 "left" 7 34.7
efg 16.8 5 "right" 0 56.7
baf 32.8 7 "bottom" 5 79.8
cef 16.8 4 "right" 0 89.7

Thank you & best regards,
ABC

Boon Chong Ang Guest

2. ## Re: sort from the smallest number to the highest number

On Fri, Jan 30, 2004 at 01:17:38PM +0800, Boon Chong Ang wrote:
> Just say I want to sort the row by using the fifth column data as
> reference from the smallest the largest number, if using sort,
In perldoc -f sort:
sort SUBNAME LIST
sort BLOCK LIST
so you need to either write a sub that will do the comparison, or an
inline block of code. The perldoc for sort is large, and mostly composed
of examples that will make it clear.

--
Robin <robinkallisti.net.nz> JabberID: <eythianjabber.org>

Hostes alienigeni me abduxerunt. Qui annus est?

PGP Key 0x776DB663 Fingerprint=DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAGeuCzTjgendttmMRAngDAJ0Zr2YEw/cdVW0N4gMMTjV+PEWVpACdFJFZ
3W8sMSQqvWJPh1vHXFPduwI=
=t4at
-----END PGP SIGNATURE-----

Robin Sheat Guest

3. ## RE: sort from the smallest number to the highest number

Hi,

Here is an old answer to your problem from Randy W. Sims.
Hope it will helps.

Perhaps you would have to modify the sort to have an numerical.

Michel S

-----Message d'origine-----
De: Randy W. Sims [mailto:RandySThePierianSpring.org]
Date: vendredi 23 janvier 2004 10:53
À: Bjorn Van Blanckenberg
Cc: Perl Beginners
Objet: Re: Reading tab delimited File & sort everything according item 5
of every line

On 01/23/04 03:36, Bjorn Van Blanckenberg wrote:
> the thing i'am looking for is that it is sorted by item 5 and writes
> back to the file
> with an extra line if item 5 is different
A variation on the Swartzian transform: (read from bottom up)

#!/usr/bin/perl

use strict;
use warnings;

print map { \$_->[1] . "\n" }
sort {\$a->[0] cmp \$b->[0]}
map { chomp; [(split /\t/, \$_)[4], \$_] }
<DATA>;

__DATA__
one title state name code1 number
two title2 state2 name2 code2 number2
one title3 state3 name3 code3 number3
four title4 state4 name4 code4 number4
six title5 state5 name5 code1 number5
dip title6 state6 name6 code1 number6
fun title7 state7 name7 code2 number7

-----Message d'origine-----
De: Boon Chong Ang [mailto:BCANGaltera.com]
Date: vendredi 30 janvier 2004 06:18
À: Hanson, Rob; [email]beginnersperl.org[/email]
Objet: sort from the smallest number to the highest number

Hi
Just say I want to sort the row by using the fifth column data as reference
from the smallest the largest number, if using sort,
It will give me result like this

Abc 12.8 8 "left" 1 1.7
Def 13.8 9 "top" 0 19.7
gef 14.8 9 "left" 0 19.7
Dgf 12.3 9 "right" 4 2.6
bac 12.8 8 "left" 1 3.7
etg 12.8 2 "left" 7 34.7
efg 16.8 5 "right" 0 56.7
baf 32.8 7 "bottom" 5 79.8
cef 16.8 4 "right" 0 89.7

but what I want is like this, what so I do to achieve this effect?

Abc 12.8 8 "left" 1 1.7
Dgf 12.3 9 "right" 4 2.6
bac 12.8 8 "left" 1 3.7
Def 13.8 9 "top" 0 19.7
gef 14.8 9 "left" 0 19.7
etg 12.8 2 "left" 7 34.7
efg 16.8 5 "right" 0 56.7
baf 32.8 7 "bottom" 5 79.8
cef 16.8 4 "right" 0 89.7

Thank you & best regards,
ABC

--
To unsubscribe, e-mail: [email]beginners-unsubscribeperl.org[/email]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Eurospace Szarindar Guest

4. ## Re: sort from the smallest number to the highest number

On Jan 30, Boon Chong Ang said:
>Just say I want to sort the row by using the fifth column data as
>reference from the smallest the largest number
>Abc 12.8 8 "left" 1 1.7
>Dgf 12.3 9 "right" 4 2.6
>bac 12.8 8 "left" 1 3.7
>Def 13.8 9 "top" 0 19.7
>gef 14.8 9 "left" 0 19.7
>etg 12.8 2 "left" 7 34.7
>efg 16.8 5 "right" 0 56.7
>baf 32.8 7 "bottom" 5 79.8
>cef 16.8 4 "right" 0 89.7
I suggest you search the web for "schwartzian transform", because that's
what I'm about to show you:

my sorted =
map \$_->[0], # get back the original string
sort { \$a->[6] <=> \$b->[6] } # sort on 5th field (0 is the original)
map [ \$_, split ], # array ref [string, fields]
data;

Read it from the bottom up.

--
Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]

Jeff 'Japhy' Pinyan Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•