<?xml version="1.0" encoding="windows-1252"?>
<node id="614814" title="Conditional Sorting" created="2007-05-11 01:06:16" updated="2007-05-10 21:06:16">
<type id="115">
perlquestion</type>
<author id="393886">
monkfan</author>
<data>
<field name="doctext">
Dear all,&lt;br&gt;

Suppose I have the following array and hash:

&lt;code&gt;
my @arr = ('foo','bar','qux','foo1','bar1','qux1');

my $hash = {
          '4-5' =&gt; '0.750',
          '0-4' =&gt; '0.167',
          '0-2' =&gt; '0.600',
          '2-3' =&gt; '0.200',
          '2-4' =&gt; '0.300',
          '0-3' =&gt; '0.300',
          '2-5' =&gt; '0.400',
          '1-2' =&gt; '0.550',
          '1-5' =&gt; '0.273',
          '3-4' =&gt; '0.300',
          '1-4' =&gt; '0.182',
          '0-1' =&gt; '0.917',
          '3-5' =&gt; '0.400',
          '0-5' =&gt; '0.250',
          '1-3' =&gt; '0.300'
        };

&lt;/code&gt;

Now the key and value of  &lt;code&gt;$hash&lt;/code&gt; represent the following:
&lt;code&gt;
    'x-y' =&gt; 'VALUE'

     x and y represent the index of the element in @arr
     x and y are reversible

     so for example:

     '0-3' =&gt; '0.300', means that
     'foo' and 'foo1' has value 0.300 OR
     'foo1' and 'foo' has value 0.300
&lt;/code&gt;

Now my question is, how can we sort decendingly &lt;code&gt;@arr_sorted&lt;/code&gt;, such that for every index 'z' in &lt;code&gt;@arr&lt;/code&gt;, the VALUE of its (z,y) &lt;= (z,y+1)?&lt;br&gt;&lt;br&gt;

Members of &lt;code&gt;@arr_sorted&lt;/code&gt; is exactly the same as 
&lt;code&gt;@arr&lt;/code&gt;, only sorted according to the condition above. And yes, every 'z' has its corresponding &lt;code&gt;@arr_sorted&lt;/code&gt;.&lt;br&gt;&lt;br&gt;

I'm not sure how to proceed from here:
&lt;code&gt;

foreach my $z (0 .. ($#arr-1)) {

   my @arr_sorted = sort{???} @arr;

}

&lt;/code&gt;

&lt;b&gt;Updated&lt;/b&gt;.

&lt;div class="pmsig"&gt;&lt;div class="pmsig-393886"&gt;
&lt;br&gt;Regards,&lt;br&gt;
Edward
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
