# effective sort on two fields in both searchorders (no FAQ !)

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

\$val={a=>[1,2],
b=>[5,9],
c=>[2,4],
...}

I need to sort the keys on the two fields (first field and then second
field), so I use:

my @sort1= sort {
\$val->->[0] <=> \$val->->[0] ||
\$val->->[1] <=> \$val->->[1]
} keys %\$val;

So far so good. Now I need - in the very next step - to do a second sort
with opposite sort-order (second field and then first field)

my @sort2= sort {
\$val->->[1] <=> \$val->->[1] ||
\$val->->[0] <=> \$val->->[0]
} keys %\$val;

The true \$val is very big and I'm sure there is a much more efficient
(and complex) way to get both searchorders in one step without doing all
the <=>-operations twice.

thnx for any ideas,
peter

--
http://www2.goldfisch.at/know_list

## Re: effective sort on two fields in both searchorders (no FAQ !)

>
>
> \$val={a=>[1,2],
>        b=>[5,9],
>        c=>[2,4],
>        ...}
>
> I need to sort the keys on the two fields (first field and then second
> field), so I use:
>
> my @sort1= sort {
>                   \$val->->[0] <=> \$val->->[0] ||
>                   \$val->->[1] <=> \$val->->[1]
>                  } keys %\$val;
>
> So far so good. Now I need - in the very next step - to do a second sort
> with opposite sort-order (second field and then first field)
>

perldoc -f reverse

hth,

James

## Re: effective sort on two fields in both searchorders (no FAQ !)

James Tolley wrote:
>>
>>So far so good. Now I need - in the very next step - to do a second sort
>>with opposite sort-order (second field and then first field)
>
> perldoc -f reverse
>

thnx, but

I dont need the reversed sort-order, I need a different sort-order where
the first sort-criteria is the second field and the second sort-criteria
is the first field.

As I said: its no FAQ

p

> hth,
>
> James
>
>

--
http://www2.goldfisch.at/know_list

## Re: effective sort on two fields in both searchorders (no FAQ !)

> \$val={a=>[1,2],
>        b=>[5,9],
>        c=>[2,4],
>        ...}
>
> I need to sort the keys on the two fields (first field and then second
> field), so I use:
>
> my @sort1= sort {
>                   \$val->->[0] <=> \$val->->[0] ||
>                   \$val->->[1] <=> \$val->->[1]
>                  } keys %\$val;
>
> So far so good. Now I need - in the very next step - to do a second sort
> with opposite sort-order (second field and then first field)
>
> my @sort2= sort {
>                   \$val->->[1] <=> \$val->->[1] ||
>                   \$val->->[0] <=> \$val->->[0]
>                  } keys %\$val;
>
> The true \$val is very big and I'm sure there is a much more efficient
> (and complex) way to get both searchorders in one step without doing all
> the <=>-operations twice.

I very much doubt it.  You could caches some of the comparisons, but the
caching overhead would probably be much greater than the savings.

Xho

--
Usenet Newsgroup Service                        \$9.95/Month 30GB

## Re: effective sort on two fields in both searchorders (no FAQ !)

peter pilsl wrote:
>
>
> \$val={a=>[1,2],
>       b=>[5,9],
>       c=>[2,4],
>       ...}
>
> I need to sort the keys on the two fields (first field and then second
> field), so I use:
>
> my @sort1= sort {
>                  \$val->->[0] <=> \$val->->[0] ||
>                  \$val->->[1] <=> \$val->->[1]
>                 } keys %\$val;
>
> So far so good. Now I need - in the very next step - to do a second sort
> with opposite sort-order (second field and then first field)
>
> my @sort2= sort {
>                  \$val->->[1] <=> \$val->->[1] ||
>                  \$val->->[0] <=> \$val->->[0]
>                 } keys %\$val;
>
>
> The true \$val is very big and I'm sure there is a much more efficient
> (and complex) way to get both searchorders in one step without doing all
> the <=>-operations twice.

I do not think you are going to easily find any way to make it more
efficient.

## Re: effective sort on two fields in both searchorders (no FAQ !)

>
>
> \$val={a=>[1,2],
>        b=>[5,9],
>        c=>[2,4],
>        ...}
>
> I need to sort the keys on the two fields (first field and then second
> field), so I use:
>
> my @sort1= sort {
>                   \$val->->[0] <=> \$val->->[0] ||
>                   \$val->->[1] <=> \$val->->[1]
>                  } keys %\$val;
>
> So far so good. Now I need - in the very next step - to do a second sort
> with opposite sort-order (second field and then first field)
>
> my @sort2= sort {
>                   \$val->->[1] <=> \$val->->[1] ||
>                   \$val->->[0] <=> \$val->->[0]
>                  } keys %\$val;
>
>
> The true \$val is very big and I'm sure there is a much more efficient
> (and complex) way to get both searchorders in one step without doing all
> the <=>-operations twice.

In general, the result of one sort says *nothing* about the other.  If
no two elements of the list compare equal in the first comparison, the
other comparison doesn't even happen.  You can't use one sort to speed
up the other.

Anno