How to sort 2 arrays and keep indices ordered?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default How to sort 2 arrays and keep indices ordered?

    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 "../open_pages/$_";
    push(@time, $time);

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

    push(@Cemails, $Cemail);

    my$row = qq{<TR><TD align="center">$Cemail</TD><TD align="center">
    $lastchange</TD><TD align="center"><A class="main_LNK" href="./view_page.pl?fl=$Cemail&
    st=oo">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 this is understandable to everyone,
    Thanks

    Motherofperls@aol.com Guest

  2. Similar Questions and Discussions

    1. #40224 [NEW]: Could PHP leave superglobal array indices alone?
      From: joem at tempomg dot com Operating system: redhat linux PHP version: 5.2.0 PHP Bug Type: Feature/Change Request Bug...
    2. Sort a hash based on values in the hash stored as arrays of hashes
      Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {...
    3. 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.
      Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp...
    4. Sort Multiple Arrays from MySQL DB
      Hi, I have the following code: <PRE> <?php $username = "####t"; $password = "####"; $hostname = "####"; mysql_connect($hostname,...
    5. #23604 [Ver->Bgs]: Problem with negative indices
      ID: 23604 Updated by: iliaa@php.net Reported By: nicolas at van-lancker dot be -Status: Verified +Status: ...
  3. #2

    Default Re: How to sort 2 arrays and keep indices ordered?


    On Sunday, September 28, 2003, at 11:47 AM, [email]Motherofperls@aol.com[/email]
    wrote:
    > 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);
    What about something like this (untested code):

    # first, we merge the data into one array
    my @merge;
    push @merge, [$times[$_], $emails[$_]] foreach (0..$#times);

    # some sort code here
    sort { $$a[1] cmp $$b[1] } @merge; # alphabetical by @emails
    # or sort { $$a[0] <=> $$b[0] } @merge; for numerical by @times

    # finally, we restore the original arrays
    @times = map { $$_[0] } @merge;
    @emails = map { $$_[1] } @merge;

    Hope that helps.

    James
    > my@rows = ();
    > for(my$i=0;$i<@open_pages;$i++){
    > my$time = -M "../open_pages/$_";
    > push(@time, $time);
    >
    > #this code is just for a readable time description,
    > my$lastchange ="";
    > if($time>1){s/\.\d*//;if($time>1){$lastchange = "$time
    > days";}else{$lastchange = "$time day";}}
    > if($time<1){$time = $time*24;if($time>
    > =2){if(/\.\d*/){s/\.\d*//;}$lastchange = "$time hours";}else{$time =
    > $time*60;if(/\.\d/){s/\.\d*//}$lastchange =
    > "$time minutes";}}
    > $lastchange =~ s/\..*\d(.*)/$1/;
    >
    > push(@Cemails, $Cemail);
    >
    > my$row = qq{<TR><TD align="center">$Cemail</TD><TD align="center">
    > $lastchange</TD><TD align="center"><A class="main_LNK"
    > href="./view_page.pl?fl=$Cemail&
    > st=oo">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 this is understandable to everyone,
    > Thanks
    James Edward Gray II Guest

  4. #3

    Default Re: How to sort 2 arrays and keep indices ordered?

    [email]Motherofperls@aol.com[/email] wrote:
    >
    > Hi all,
    Hello,
    > 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);
    I would use an array of arrays to keep the two fields synced.

    # Array of Arrays
    @rows = ( [ email1 => time1 ],
    [ email2 => time2 ],
    ...
    [ emailn => timen ],
    );


    # Sorting an Array of Arrays
    my @sorted;
    if ( $q->param( 'oldest' ) or $q->param( 'newest' ) ) {
    @sorted = sort { $a->[ 1 ] <=> $b->[ 1 ] } @rows;
    if ( $q->param( 'oldest' ) ) {
    @sorted = reverse @sorted;
    }
    }
    elsif ( $q->param( 'AtoZ' ) or $q->param( 'ZtoA' ) ) {
    @sorted = sort { $a->[ 0 ] cmp $b->[ 0 ] } @rows;
    if ( $q->param( 'ZtoA' ) ) {
    @sorted = reverse @sorted;
    }
    }


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139