作者kevin0523 (Kevin)
看板PHP
标题[请益] 关联性阵列排序问题
时间Thu Apr 19 11:25:51 2007
关联性阵列排序问题
Given an array in
PHP : $ list = array('apple'=>300, 'orange'=>150, 'banana'=>100,
'mango'=>330);
sort the array by the order of key and value respectively with your own
sorting algorithm.
Please don't use the sorting functions provided by language.
请问各位高手该如何写出来呢? 不能使用ksort,rsort,等 php内建sort函式
想破头了 想不出来
若不是关联性阵列可使用气泡排序 可是关联性我就写不出来了,有人可以帮帮忙吗?感谢
~
$a = array(2,30,1,75,65,32,24,19,3,60,51) ;
$ln = count($a ) ;
for ($i = 0 ; $i <$ln; $i++) {
echo $a[$i] . "," ;
}
echo "<br>" ;
//气泡排序-------------------------------------------------------------------
echo "气泡排序<br>" ;
for ($j= $ln ; $j>1 ; $j--) {
for ($i= 0 ; $i< $j-1 ; $i++) {
if ($a[$i]>$a[$i+1]) {
//echo $a[$i] ."--". $a[$i+1] ."<br>" ;
$temp = $a[$i+1] ;
$a[$i+1] = $a[$i];
$a[$i]= $temp ;
//echo $a[$i] . " , " . $a[$i+1] . " <br>" ;
}
}
}
for ($i = 0 ; $i <$ln; $i++) {
echo $a[$i] . "," ;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.105.128.151
1F:推 foxzgerald:这是要分别对 key 和 value 排序嘛? 04/20 01:46