473,382 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

preserve sort order in another list

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.
Jul 19 '05 #1
4 5297
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
Jul 19 '05 #2
"Jürgen Exner" <ju******@hotmail.com> 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?
Jul 19 '05 #3
In article <XZ********************@adelphia.com>, Brett
<bg*****@hotmail.com> wrote:
"Jürgen Exner" <ju******@hotmail.com> 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 =---
Jul 19 '05 #4
"Jim Gibson" <jg*****@mail.arc.nasa.gov> wrote in message
news:050520051241241521%jg*****@mail.arc.nasa.gov. ..
In article <XZ********************@adelphia.com>, Brett
<bg*****@hotmail.com> wrote:
"Jürgen Exner" <ju******@hotmail.com> 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.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: googleboy | last post by:
I didn't think this would be as difficult as it now seems to me. I am reading in a csv file that documents a bunch of different info on about 200 books, such as title, author, publisher, isbn,...
11
by: James P. | last post by:
Hello, I have a report with the Priority field is used as sort order and grouping. The problem is the data in this Priority field if sorted in ascending order is: High, Low, and Medium. How...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
7
by: Majnu | last post by:
Hi community, just in case somebody needs a shellsort in c#, I rewrote the pascal code that I found in another newsgroup. Here are both. For more explanation on the pascal code you can search...
11
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them...
2
by: Gummy | last post by:
Hello All, I have a webpage that has two dropdown listboxes. Based on what is selected in these dropdown listboxes, it filters a DataGrid . That works fine. In the DataGrid , when I go to edit...
6
by: Mark | last post by:
In a specification I'm designing, I'd like to specify that the order of Names (IDs) in an attribute value of datatype IDREFS is significant. My question is if most real-world XML parsers...
18
by: xahlee | last post by:
Last year, i've posted a tutorial and commentary about Python and Perl's sort function. (http://xahlee.org/perl-python/sort_list.html) In that article, i discussed a technique known among...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.