Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > preserve sort order in another list

Reply
Thread Tools

preserve sort order in another list

 
 
Brett
Guest
Posts: n/a
 
      05-05-2005
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.


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      05-05-2005
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


 
Reply With Quote
 
 
 
 
Brett
Guest
Posts: n/a
 
      05-05-2005
"Jürgen Exner" <(E-Mail Removed)> 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?


 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      05-05-2005
In article <(E-Mail Removed)>, Brett
<(E-Mail Removed)> wrote:

> "Jürgen Exner" <(E-Mail Removed)> 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 =---
 
Reply With Quote
 
Brett
Guest
Posts: n/a
 
      05-05-2005
"Jim Gibson" <(E-Mail Removed)> wrote in message
news:050520051241241521%(E-Mail Removed). ..
> In article <(E-Mail Removed)>, Brett
> <(E-Mail Removed)> wrote:
>
> > "Jürgen Exner" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
How do you sort a name list and list in random order? Frank & Janny Plaza Python 4 09-23-2003 01:51 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM
FileUtils - :preserve does not preserve mtime of directories on Windoze Grzegorz Chrupala Ruby 2 06-30-2003 07:38 AM



Advertisments