Skip to content
This repository
Browse code

Fix sort() not working.

  • Loading branch information...
commit 771efd950e12db153e9a00b9af8e6507d82be36c 1 parent 57d50cd
Mark Story authored February 19, 2012
31  lib/Cake/Test/Case/Utility/Set2Test.php
@@ -804,7 +804,6 @@ public function testExtractAttributePattern() {
804 804
  * @return void
805 805
  */
806 806
 	public function testSort() {
807  
-		$this->markTestIncomplete('Not done, sort() is broken.');
808 807
 		$a = array(
809 808
 			0 => array(
810 809
 				'Person' => array('name' => 'Jeff'),
@@ -883,36 +882,6 @@ public function testSort() {
883 882
 		$this->assertEquals($a, $b);
884 883
 
885 884
 		$a = array(
886  
-			array(7,6,4),
887  
-			array(3,4,5),
888  
-			array(3,2,1),
889  
-		);
890  
-
891  
-		$b = array(
892  
-			array(3,2,1),
893  
-			array(3,4,5),
894  
-			array(7,6,4),
895  
-		);
896  
-
897  
-		$a = Set2::sort($a, '{n}.{n}', 'asc');
898  
-		$this->assertEquals($a, $b);
899  
-
900  
-		$a = array(
901  
-			array(7,6,4),
902  
-			array(3,4,5),
903  
-			array(3,2,array(1,1,1)),
904  
-		);
905  
-
906  
-		$b = array(
907  
-			array(3,2,array(1,1,1)),
908  
-			array(3,4,5),
909  
-			array(7,6,4),
910  
-		);
911  
-
912  
-		$a = Set2::sort($a, '{n}', 'asc');
913  
-		$this->assertEquals($a, $b);
914  
-
915  
-		$a = array(
916 885
 			0 => array('Person' => array('name' => 'Jeff')),
917 886
 			1 => array('Shirt' => array('color' => 'black'))
918 887
 		);
11  lib/Cake/Utility/Set2.php
@@ -538,7 +538,16 @@ public static function sort(array $data, $path, $dir) {
538 538
 		if (is_numeric(implode('', $originalKeys))) {
539 539
 			$data = array_values($data);
540 540
 		}
541  
-		$result = self::_squash(self::extract($data, $path));
  541
+		$sortValues = self::extract($data, $path);
  542
+		$sortCount = count($sortValues);
  543
+		$dataCount = count($data);
  544
+
  545
+		// Make sortValues match the data length, as some keys could be missing
  546
+		// the sorted value path.
  547
+		if ($sortCount < $dataCount) {
  548
+			$sortValues = array_pad($sortValues, $dataCount, null);
  549
+		}
  550
+		$result = self::_squash($sortValues);
542 551
 		$keys = self::extract($result, '{n}.id');
543 552
 		$values = self::extract($result, '{n}.value');
544 553
 

0 notes on commit 771efd9

Please sign in to comment.
Something went wrong with that request. Please try again.