Newbie question sorting

Discussion in 'Perl Misc' started by mdfoster44@netscape.net, Feb 18, 2005.

  1. Guest

    Hi

    Could someone please point me to some documentation on how to sort
    arrays such as these?

    a[0][0]= ANA a[0][1]= 1
    a[1][0]= MFI a[1][1]= 12
    a[2][0]= ABW a[2][1]= 1
    a[3][0]= LTL a[3][1]= 2
    a[4][0]= APD a[4][1]= 2

    I would like to sort the first column after sorting the second column
    numerically.
    So the sort of the above data should give back.

    ABW 1
    ANA 1
    APD 2
    LTL 2
    MFI 12

    Thanks
     
    , Feb 18, 2005
    #1
    1. Advertising

  2. phaylon Guest

    mdfoster44 wrote:

    > Could someone please point me to some documentation on how to sort arrays
    > such as these?


    Sure: 'perldoc -f sort'.

    hth,phay

    --
    http://www.dunkelheit.at/
    thou shallst fear...
     
    phaylon, Feb 18, 2005
    #2
    1. Advertising

  3. wrote:
    > Could someone please point me to some documentation on how to sort
    > arrays such as these?
    >
    > a[0][0]= ANA a[0][1]= 1
    > a[1][0]= MFI a[1][1]= 12
    > a[2][0]= ABW a[2][1]= 1
    > a[3][0]= LTL a[3][1]= 2
    > a[4][0]= APD a[4][1]= 2


    Which programming language are you dealing with? That does not look like
    Perl code to me.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 18, 2005
    #3
  4. Paul Lalli Guest

    <> wrote in message
    news:...

    > Could someone please point me to some documentation on how to sort


    perldoc -f sort

    > arrays such as these?
    >
    > a[0][0]= ANA a[0][1]= 1
    > a[1][0]= MFI a[1][1]= 12
    > a[2][0]= ABW a[2][1]= 1
    > a[3][0]= LTL a[3][1]= 2
    > a[4][0]= APD a[4][1]= 2


    What language is this?

    > I would like to sort the first column


    How are you defining 'column'?

    > after sorting the second column numerically.
    > So the sort of the above data should give back.
    >
    > ABW 1
    > ANA 1
    > APD 2
    > LTL 2
    > MFI 12
    >


    #!/usr/bin/perl
    use strict;
    use warnings;
    my @data = (
    ['ANA', 1],
    ['MFI', 12],
    ['ABW', 1],
    ['LTL', 2],
    ['APD', 2],);

    print "$_->[0] $_->[1]\n" for sort {$a->[1] <=> $b->[1] or $a->[0] cmp
    $b->[0]} @data;

    __END__
    ABW 1
    ANA 1
    APD 2
    LTL 2
    MFI 12



    Paul Lalli
     
    Paul Lalli, Feb 18, 2005
    #4
  5. Guest

    Paul Lalli wrote:
    > <> wrote in message
    > news:...
    >
    > > Could someone please point me to some documentation on how to sort

    >
    > perldoc -f sort
    >
    > > arrays such as these?
    > >
    > > a[0][0]= ANA a[0][1]= 1
    > > a[1][0]= MFI a[1][1]= 12
    > > a[2][0]= ABW a[2][1]= 1
    > > a[3][0]= LTL a[3][1]= 2
    > > a[4][0]= APD a[4][1]= 2

    >
    > What language is this?
    >
    > > I would like to sort the first column

    >
    > How are you defining 'column'?
    >
    > > after sorting the second column numerically.
    > > So the sort of the above data should give back.
    > >
    > > ABW 1
    > > ANA 1
    > > APD 2
    > > LTL 2
    > > MFI 12
    > >

    >
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > my @data = (
    > ['ANA', 1],
    > ['MFI', 12],
    > ['ABW', 1],
    > ['LTL', 2],
    > ['APD', 2],);
    >
    > print "$_->[0] $_->[1]\n" for sort {$a->[1] <=> $b->[1] or $a->[0]

    cmp
    > $b->[0]} @data;


    Thank you for your reply.

    Can something similar work for a reference to an array of references to
    arrays?
    I using a "$data" from a mysql query.

    my $data = $sth->fetchall_arrayref;

    so then the data is actually
    $data->[0][0] = ANA
    $data->[0][1] = 1
    $data->[1][0] = MFI and so forth.

    Thanks again for taking the time to help.

    Martin Foster.

    >
    > __END__
    > ABW 1
    > ANA 1
    > APD 2
    > LTL 2
    > MFI 12
    >
    >
    >
    > Paul Lalli
     
    , Feb 18, 2005
    #5
  6. wrote in news:1108764160.887577.83230
    @g14g2000cwa.googlegroups.com:

    > Can something similar work for a reference to an array of references
    > to arrays?
    > I using a "$data" from a mysql query.
    >
    > my $data = $sth->fetchall_arrayref;


    Why don't you write the query to return the results in the order you want?

    Sinan.
     
    A. Sinan Unur, Feb 18, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hwg
    Replies:
    2
    Views:
    218
    Paul McGuire
    Aug 29, 2007
  2. Replies:
    2
    Views:
    1,440
    James Kanze
    Jul 6, 2010
  3. Jason
    Replies:
    0
    Views:
    390
    Jason
    Oct 4, 2006
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    476
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    161
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page