Browse Source

Fix sort() not working.

mark_story 14 years ago
parent
commit
771efd950e
2 changed files with 10 additions and 32 deletions
  1. 0 31
      lib/Cake/Test/Case/Utility/Set2Test.php
  2. 10 1
      lib/Cake/Utility/Set2.php

+ 0 - 31
lib/Cake/Test/Case/Utility/Set2Test.php

@@ -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'),
@@ -883,36 +882,6 @@ class Set2Test extends CakeTestCase {
 		$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'))
 		);

+ 10 - 1
lib/Cake/Utility/Set2.php

@@ -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');