Reply to Sorting arrays

Your name:

Reply:


Posted by Paul Lautman on 05/21/06 01:46

I'm having some trouble understanding what is happening with some array
sorting functions. In all cases, my compare function is:
function compare($x, $y)
{
if ( $x[1] == $y[1] )
return 0;
else if ( $x[1] < $y[1] )
return -1;
else
return 1;
}
I started with this:
$contents = array( array( 'TIR', 'Tires', 100 ),
array( 'OIL', 'Oil', 10 ),
array( 'SPK', 'Spark Plugs', 4 ) );
echo '<br>XXX<br>';
foreach($contents as $r) {
var_export($r);
echo '<br>';
}
echo '<br>YYY<br>';
uasort($contents, 'compare');
foreach($contents as $r) {
var_export($r);
echo '<br>';
}
And the output, whether I use usort or uasort is always:
XXX
array ( 0 => 'TIR', 1 => 'Tires', 2 => 100, )
array ( 0 => 'OIL', 1 => 'Oil', 2 => 10, )
array ( 0 => 'SPK', 1 => 'Spark Plugs', 2 => 4, )

YYY
array ( 0 => 'OIL', 1 => 'Oil', 2 => 10, )
array ( 0 => 'SPK', 1 => 'Spark Plugs', 2 => 4, )
array ( 0 => 'TIR', 1 => 'Tires', 2 => 100, )
But if I use uksort I get:
XXX
array ( 0 => 'TIR', 1 => 'Tires', 2 => 100, )
array ( 0 => 'OIL', 1 => 'Oil', 2 => 10, )
array ( 0 => 'SPK', 1 => 'Spark Plugs', 2 => 4, )

YYY
array ( 0 => 'SPK', 1 => 'Spark Plugs', 2 => 4, )
array ( 0 => 'OIL', 1 => 'Oil', 2 => 10, )
array ( 0 => 'TIR', 1 => 'Tires', 2 => 100, )

But I cannot see why I get this reversal of order???

I then tried:
$contents = array( array( c=>'TIR', d=>'Tires', p=>100 ),
array( c=>'OIL', d=>'Oil', p=>10 ),
array( c=>'SPK', d=>'Spark Plugs', p=>4 ) );

echo '<br>XXX<br>';
foreach($contents as $r) {
var_export($r);
echo '<br>';
}
echo '<br>YYY<br>';
uksort($contents, 'compare');
foreach($contents as $r) {
var_export($r);
echo '<br>';
}

And this time the output, whether I use usort, uasort or uksort is always:
XXX
array ( 'c' => 'TIR', 'd' => 'Tires', 'p' => 100, )
array ( 'c' => 'OIL', 'd' => 'Oil', 'p' => 10, )
array ( 'c' => 'SPK', 'd' => 'Spark Plugs', 'p' => 4, )

YYY
array ( 'c' => 'SPK', 'd' => 'Spark Plugs', 'p' => 4, )
array ( 'c' => 'OIL', 'd' => 'Oil', 'p' => 10, )
array ( 'c' => 'TIR', 'd' => 'Tires', 'p' => 100, )

Finally I tried:
$contents = array( array( c=>'TIR', d=>'Tires', p=>100 ),
array( c=>'OIL', a=>'Oil', p=>10 ),
array( c=>'SPK', z=>'Spark Plugs', p=>4 ) );

echo '<br>XXX<br>';
foreach($contents as $r) {
var_export($r);
echo '<br>';
}
echo '<br>YYY<br>';
uksort($contents, 'compare');
foreach($contents as $r) {
var_export($r);
echo '<br>';
}
And once again, the outputs with usort, uasort or uksort is always:
XXX
array ( 'c' => 'TIR', 'd' => 'Tires', 'p' => 100, )
array ( 'c' => 'OIL', 'd' => 'Oil', 'p' => 10, )
array ( 'c' => 'SPK', 'd' => 'Spark Plugs', 'p' => 4, )

YYY
array ( 'c' => 'SPK', 'd' => 'Spark Plugs', 'p' => 4, )
array ( 'c' => 'OIL', 'd' => 'Oil', 'p' => 10, )
array ( 'c' => 'TIR', 'd' => 'Tires', 'p' => 100, )

Can someone explain why none of the cases will sort the associative array by
the second "column" and how I can find examples that will sow the
differences between the 3 sort functions???

TIA
Paul

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация