Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting 2 arrays and maintaining relating indices

Status
Not open for further replies.

webscripter

Programmer
Jul 29, 2002
266
US
Hi all,

Is there an easy way to sort 2 arrays and keep the relating indices together.
for instance I have an array of times, and an array of emails.
I would like to give the user a choice of sorting by time(numeral) or email(alpabetical);

my@rows = ();
for(my$i=0;$i<@open_pages;$i++){
my$time = -M &quot;../open_pages/$_&quot;;
push(@time, $time);

#this code is just for a readable time description,
my$lastchange =&quot;&quot;;
if($time>1){s/\.\d*//;if($time>1){$lastchange = &quot;$time days&quot;;}else{$lastchange = &quot;$time day&quot;;}}
if($time<1){$time = $time*24;if($time>=2){if(/\.\d*/){s/\.\d*//;}$lastchange = &quot;$time hours&quot;;}else{$time = $time*60;if(/\.\d/){s/\.\d*//}$lastchange = &quot;$time minutes&quot;;}}
$lastchange =~ s/\..*\d(.*)/$1/;


push(@Cemails, $Cemail);

my$row = qq{<TR><TD align=&quot;center&quot;>$Cemail</TD><TD align=&quot;center&quot;>$lastchange</TD><TD align=&quot;center&quot;><A class=&quot;main_LNK&quot; href=&quot;./view_page.pl?fl=$Cemail&st=oo&quot;>view</A></TD></TR>};

# @rows is the array I want to rearrange
push(@rows,$rows});
}

# here I would like to sort @rows according to the method they chose
# but I'm not sure how to sort 2 arrays at the same time without using some sort of grep for the indices.

my@sorted = ();
if($q->param('oldest') || $q->param('newest')){
@sorted = sort($a<=>$b) @times;
# change @rows here somehow with @times
if($q->param('oldest)){@sorted = reverse @sorted;}
}

elsif($q->param('AtoZ') || $q->param('ZtoA')){
@sorted = sort($a cmp $b) @Cemails;
# change @rows here somehow with @Cemails
if($q->param('ZtoA')){@rows= reverse @rows}
}


Hope there is an easy answer!

Thanks
Pat
admin@websiteprogrammin.com
 
Hi Pat,

I would be inclined to store my data as a multi-dimensional array first - i.e.

my @data = ( [ 10234532, 'email information'],
[ 10235432, 'another email'],
[ 20345234, 'yet another email'] )

then you can sort on time as follows:

@sorted = sort {$a->[0] <=> $b->[0]} @data;

or email:

@sorted = sort {$a->[1] <=> $b->[1]} @data;

does this help any?

Scotty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top