Fix sort() not working.

This commit is contained in:
mark_story 2012-02-19 23:05:38 -05:00
parent 57d50cd018
commit 771efd950e
2 changed files with 10 additions and 32 deletions

View file

@ -804,7 +804,6 @@ class Set2Test extends CakeTestCase {
* @return void
*/
public function testSort() {
$this->markTestIncomplete('Not done, sort() is broken.');
$a = array(
0 => array(
'Person' => array('name' => 'Jeff'),
@ -882,36 +881,6 @@ class Set2Test extends CakeTestCase {
$a = Set2::sort($a, '{n}.Person.name', 'asc');
$this->assertEquals($a, $b);
$a = array(
array(7,6,4),
array(3,4,5),
array(3,2,1),
);
$b = array(
array(3,2,1),
array(3,4,5),
array(7,6,4),
);
$a = Set2::sort($a, '{n}.{n}', 'asc');
$this->assertEquals($a, $b);
$a = array(
array(7,6,4),
array(3,4,5),
array(3,2,array(1,1,1)),
);
$b = array(
array(3,2,array(1,1,1)),
array(3,4,5),
array(7,6,4),
);
$a = Set2::sort($a, '{n}', 'asc');
$this->assertEquals($a, $b);
$a = array(
0 => array('Person' => array('name' => 'Jeff')),
1 => array('Shirt' => array('color' => 'black'))

View file

@ -538,7 +538,16 @@ class Set2 {
if (is_numeric(implode('', $originalKeys))) {
$data = array_values($data);
}
$result = self::_squash(self::extract($data, $path));
$sortValues = self::extract($data, $path);
$sortCount = count($sortValues);
$dataCount = count($data);
// Make sortValues match the data length, as some keys could be missing
// the sorted value path.
if ($sortCount < $dataCount) {
$sortValues = array_pad($sortValues, $dataCount, null);
}
$result = self::_squash($sortValues);
$keys = self::extract($result, '{n}.id');
$values = self::extract($result, '{n}.value');