<?xml version="1.0" encoding="windows-1252"?>
<node id="614816" title="Re: Conditional Sorting" created="2007-05-11 01:31:11" updated="2007-05-10 21:31:11">
<type id="11">
note</type>
<author id="170442">
jdporter</author>
<data>
<field name="doctext">
&lt;p&gt;
It appears that &lt;c&gt;@arr&lt;/c&gt; are the nodes in a graph, and &lt;c&gt;%hash&lt;/c&gt; are the weights of all the edges in the graph. (It's undirected, and fully connected.)
So now you want to sort the nodes &amp;mdash; but by what, exactly, is not clear.
With the information given, I would surmise that the values for each node come from the weights of all its edges. If so, then the following:
&lt;/p&gt;

&lt;code&gt;
use strict; use warnings;
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'
);

my @nodew;
for ( keys %hash )
{
	my $v = $hash{$_};
	$nodew[$_] += $v for /(\d+)/g;
}

my @sorted_node_indices = sort {
	$nodew[$a] &lt;=&gt; $nodew[$b]
} 0 .. $#nodew;

my @arr_sorted = @arr[ @sorted_node_indices ];
&lt;/code&gt;

&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; I see, you want, &lt;i&gt;for each node&lt;/i&gt;, the list of its neighbor nodes sorted by edge weight.
&lt;/p&gt;
&lt;code&gt;
use strict; use warnings;
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'
);

my %edgew;

for ( keys %hash )
{
	my( $from, $to ) = @arr[ /(\d+)/g ];
	$edgew{$from}{$to} = 
	$edgew{$to}{$from} = $hash{$_};
}

for my $from ( sort keys %edgew )
{
	print "$from:\n";
	print "\t$_ : $edgew{$from}{$_}\n" for 
		sort { $edgew{$from}{$a} &lt;=&gt; $edgew{$from}{$b} }
		keys %{ $edgew{$from} };
}
&lt;/code&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-170442"&gt;
&lt;small&gt;&lt;i&gt;A word spoken in Mind will reach its own level, in the objective world, by its own wei[http://www.sacred-texts.com/eso/som/som19.htm|g]ht&lt;/i&gt;&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
614814</field>
<field name="parent_node">
614814</field>
</data>
</node>
