Permalink
Browse files

Fix sort() not working.

  • Loading branch information...
1 parent 57d50cd commit 771efd950e12db153e9a00b9af8e6507d82be36c @markstory markstory committed Feb 20, 2012
Showing with 10 additions and 32 deletions.
  1. +0 −31 lib/Cake/Test/Case/Utility/Set2Test.php
  2. +10 −1 lib/Cake/Utility/Set2.php
@@ -804,7 +804,6 @@ public function testExtractAttributePattern() {
* @return void
*/
public function testSort() {
- $this->markTestIncomplete('Not done, sort() is broken.');
$a = array(
0 => array(
'Person' => array('name' => 'Jeff'),
@@ -883,36 +882,6 @@ public function testSort() {
$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
@@ -538,7 +538,16 @@ public static function sort(array $data, $path, $dir) {
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');

0 comments on commit 771efd9

Please sign in to comment.