sort from the smallest number to the highest number

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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. Similar Questions and Discussions

    1. int Number to text Number Function / UDF ?
      Is there a function that converts e.g. 3 to Three? Probably not but I just thought I'd ask.
    2. Number to hex ?
      Looking at a few scripts, too disguise ID numbers or make them random, there appears to be a conversion to an alphanumeric number. e.g....
    3. sh: bad number
      Hi, I compiled a simple program with perl. But I ran into some problems. sh: campusUnderAttack.out: bad number I try to execute a program and...
    4. Rounder A Number Up!!
      I am dividing a value by two..what i need is to round the number up if it is not a whole number. Any suggestions on how to do this ?? Help...
    5. tty-dev-number <-> /dev/pts
      Hi, I'm writing a program under Solaris (sparc) that has to know from what machine a user is logged on who is starting that program. Reading...
  3. #2

    Default 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 <robin@kallisti.net.nz> JabberID: <eythian@jabber.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

  4. #3

    Default 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:RandyS@ThePierianSpring.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:BCANG@altera.com]
    Date: vendredi 30 janvier 2004 06:18
    À: Hanson, Rob; [email]beginners@perl.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-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>

    Eurospace Szarindar Guest

  5. #4

    Default 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]japhy@pobox.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 post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139