我使用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