我使用usort排序與每個元素內的關聯數組的數組。PHP usort reorders數組的排序值是相同的所有
當我在數組中排序的所有值都相同時,它仍會更改數組中元素的位置,有沒有辦法防止這種情況?
例如這樣的:
array(
array('name' => 'Ben', 'authn_weight' => 85.3),
array('name' => 'Josh', 'authn_weight' => 85.3),
array('name' => 'Fred', 'authn_weight' => 85.3)
);
可以改變這樣:
array(
array('name' => 'Josh', 'authn_weight' => 85.3),
array('name' => 'Ben', 'authn_weight' => 85.3),
array('name' => 'Fred', 'authn_weight' => 85.3)
);
這是排序功能:
private function weightSortImplementation($a, $b){
$aWeight = $a['autn_weight'];
$bWeight = $b['autn_weight'];
if ($aWeight == $bWeight) {
return 0;
}
return ($aWeight < $bWeight) ? 1 : -1;
}
我已經檢查了weightSortImplementation
功能始終返回0表明它們是相同的。那麼爲什麼這仍然是重新排列數組?
這是一個有趣的問題。我剛剛測試過這個,在使用'usort'後,順序被顛倒過來。 http://codepad.org/PRFpq8Ug –
它們不得使用[穩定排序](http://en.wikipedia.org/wiki/Sorting_algorithm#Stability),如果它們是元素,它不會保證元素的順序等於。 – JoeyJ