preserve sort order in another list

Discussion in 'Perl' started by Brett, May 5, 2005.

  1. Brett

    Brett Guest

    I have two arrays and i wish to sort the first one numerically, but after
    sorting, I would like the second array to be in the same matching order as
    the first array.

    ie.

    @l1={3,1,2};
    @l2={'a','b','c'};

    @l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}

    # do something to @l2 to make order {'b','c','a'} (preserving the original
    mapping with the first list)

    There's probably an easy way to do this that i'm not aware of.

    Thanks in advance,
    Brett.
     
    Brett, May 5, 2005
    #1
    1. Advertising

  2. Brett wrote:
    > I have two arrays and i wish to sort the first one numerically, but
    > after sorting, I would like the second array to be in the same
    > matching order as the first array.
    >
    > ie.
    >
    > @l1={3,1,2};
    > @l2={'a','b','c'};
    >
    > @l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
    >
    > # do something to @l2 to make order {'b','c','a'} (preserving the
    > original mapping with the first list)
    >
    > There's probably an easy way to do this that i'm not aware of.


    Indeed, there is: use a data structure that matches your problem better.
    Instead of having a pair of unrelated arrays use a single array of pairs.
    And then sort that single array by the value of the first component of each
    pair.

    jue
     
    Jürgen Exner, May 5, 2005
    #2
    1. Advertising

  3. Brett

    Brett Guest

    "Jürgen Exner" <> wrote in message
    news:8Tmee.2706$Vu.1954@trnddc07...
    > Brett wrote:
    > > I have two arrays and i wish to sort the first one numerically, but
    > > after sorting, I would like the second array to be in the same
    > > matching order as the first array.
    > >
    > > ie.
    > >
    > > @l1={3,1,2};
    > > @l2={'a','b','c'};
    > >
    > > @l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
    > >
    > > # do something to @l2 to make order {'b','c','a'} (preserving the
    > > original mapping with the first list)
    > >
    > > There's probably an easy way to do this that i'm not aware of.

    >
    > Indeed, there is: use a data structure that matches your problem better.
    > Instead of having a pair of unrelated arrays use a single array of pairs.
    > And then sort that single array by the value of the first component of

    each
    > pair.
    >
    > jue



    I'm new to this, and tried to give it a go.

    struct ID =>
    {
    number => '$',
    name => '$',
    };

    my @IDs = ID->new();
    #fill data...

    @IDs = sort {$a->number <=> $b->number} (@IDs); # numeric sort on number

    but that failed to sort the list as i expected. How can i use sort to do
    what I want?
     
    Brett, May 5, 2005
    #3
  4. Brett

    Jim Gibson Guest

    In article <>, Brett
    <> wrote:

    > "Jürgen Exner" <> wrote in message
    > news:8Tmee.2706$Vu.1954@trnddc07...
    > > Brett wrote:
    > > > I have two arrays and i wish to sort the first one numerically, but
    > > > after sorting, I would like the second array to be in the same
    > > > matching order as the first array.
    > > >
    > > > ie.
    > > >
    > > > @l1={3,1,2};
    > > > @l2={'a','b','c'};
    > > >
    > > > @l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
    > > >
    > > > # do something to @l2 to make order {'b','c','a'} (preserving the
    > > > original mapping with the first list)
    > > >
    > > > There's probably an easy way to do this that i'm not aware of.

    > >
    > > Indeed, there is: use a data structure that matches your problem better.
    > > Instead of having a pair of unrelated arrays use a single array of pairs.
    > > And then sort that single array by the value of the first component of

    > each
    > > pair.
    > >
    > > jue

    >
    >
    > I'm new to this, and tried to give it a go.
    >
    > struct ID =>
    > {
    > number => '$',
    > name => '$',
    > };
    >
    > my @IDs = ID->new();
    > #fill data...
    >
    > @IDs = sort {$a->number <=> $b->number} (@IDs); # numeric sort on number
    >
    > but that failed to sort the list as i expected. How can i use sort to do
    > what I want?


    Please post a complete program so we can see where you are going wrong,
    but post it to comp.lang.perl.misc because this newsgroup is defunct.

    Here is a version that uses an array of array references to an array of
    two elements, as Jürgen suggested:

    #!/usr/local/bin/perl

    use strict;
    use warnings;

    my @l1 = qw/ 3 1 2 /;
    my @l2 = qw/ a b c /;

    my @ids = map { [ $l1[$_], $l2[$_] ] } (0..$#l1);

    print "Unsorted Array:\n";
    for my $r ( @ids ) {
    print " @$r\n";
    }
    print "\nSorted array:\n";
    foreach my $r ( sort { $a->[0] <=> $b->[0] } @ids ) {
    print " @$r\n";
    }

    __OUTPUT__

    Unsorted Array:
    3 a
    1 b
    2 c

    Sorted array:
    1 b
    2 c
    3 a


    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
     
    Jim Gibson, May 5, 2005
    #4
  5. Brett

    Brett Guest

    "Jim Gibson" <> wrote in message
    news:050520051241241521%...
    > In article <>, Brett
    > <> wrote:
    >
    > > "Jürgen Exner" <> wrote in message
    > > news:8Tmee.2706$Vu.1954@trnddc07...
    > > > Brett wrote:
    > > > > I have two arrays and i wish to sort the first one numerically, but
    > > > > after sorting, I would like the second array to be in the same
    > > > > matching order as the first array.
    > > > >
    > > > > ie.
    > > > >
    > > > > @l1={3,1,2};
    > > > > @l2={'a','b','c'};
    > > > >
    > > > > @l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
    > > > >
    > > > > # do something to @l2 to make order {'b','c','a'} (preserving the
    > > > > original mapping with the first list)
    > > > >
    > > > > There's probably an easy way to do this that i'm not aware of.
    > > >
    > > > Indeed, there is: use a data structure that matches your problem

    better.
    > > > Instead of having a pair of unrelated arrays use a single array of

    pairs.
    > > > And then sort that single array by the value of the first component of

    > > each
    > > > pair.
    > > >
    > > > jue

    > >
    > >
    > > I'm new to this, and tried to give it a go.
    > >
    > > struct ID =>
    > > {
    > > number => '$',
    > > name => '$',
    > > };
    > >
    > > my @IDs = ID->new();
    > > #fill data...
    > >
    > > @IDs = sort {$a->number <=> $b->number} (@IDs); # numeric sort on

    number
    > >
    > > but that failed to sort the list as i expected. How can i use sort to do
    > > what I want?

    >
    > Please post a complete program so we can see where you are going wrong,
    > but post it to comp.lang.perl.misc because this newsgroup is defunct.
    >
    > Here is a version that uses an array of array references to an array of
    > two elements, as Jürgen suggested:
    >
    > #!/usr/local/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my @l1 = qw/ 3 1 2 /;
    > my @l2 = qw/ a b c /;
    >
    > my @ids = map { [ $l1[$_], $l2[$_] ] } (0..$#l1);
    >
    > print "Unsorted Array:\n";
    > for my $r ( @ids ) {
    > print " @$r\n";
    > }
    > print "\nSorted array:\n";
    > foreach my $r ( sort { $a->[0] <=> $b->[0] } @ids ) {
    > print " @$r\n";
    > }
    >
    > __OUTPUT__
    >
    > Unsorted Array:
    > 3 a
    > 1 b
    > 2 c
    >
    > Sorted array:
    > 1 b
    > 2 c
    > 3 a



    Thanks, i'll report to the other group. I'd like to stick with the
    structures, since they look a bit easier.

    Brett.
     
    Brett, May 5, 2005
    #5
    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. nobody
    Replies:
    0
    Views:
    538
    nobody
    Jun 1, 2004
  2. Frank & Janny Plaza
    Replies:
    4
    Views:
    624
    Kenneth Hutson
    Sep 23, 2003
  3. =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=

    [B,IX] = sort(A,...) - Order for sort()-function

    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=, May 29, 2007, in forum: Python
    Replies:
    0
    Views:
    322
    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=
    May 29, 2007
  4. Navin
    Replies:
    1
    Views:
    699
    Ken Schaefer
    Sep 9, 2003
  5. Grzegorz Chrupala
    Replies:
    2
    Views:
    211
    Grzegorz Chrupala
    Jun 30, 2003
Loading...

Share This Page