Skip to content
This repository
Browse code

Port most recent changes in Set to Hash.

See [68eeee8] and
[af57502]
  • Loading branch information...
commit 9015d7801754145a094248efcf3ea904739b45db 1 parent e55927c
Mark Story authored March 13, 2012
37  lib/Cake/Test/Case/Utility/HashTest.php
@@ -1034,6 +1034,43 @@ public function testSortWithOutOfOrderKeys() {
1034 1034
 	}
1035 1035
 
1036 1036
 /**
  1037
+ * test sorting with string keys.
  1038
+ *
  1039
+ * @return void
  1040
+ */
  1041
+	public function testSortString() {
  1042
+		$to_sort = array(
  1043
+			'four' => array('number' => 4, 'some' => 'foursome'),
  1044
+			'six' => array('number' => 6, 'some' => 'sixsome'),
  1045
+			'five' => array('number' => 5, 'some' => 'fivesome'),
  1046
+			'two' => array('number' => 2, 'some' => 'twosome'),
  1047
+			'three' => array('number' => 3, 'some' => 'threesome')
  1048
+		);
  1049
+		$sorted = Hash::sort($to_sort, '{s}.number', 'asc');
  1050
+		$expected = array(
  1051
+			'two' => array('number' => 2, 'some' => 'twosome'),
  1052
+			'three' => array('number' => 3, 'some' => 'threesome'),
  1053
+			'four' => array('number' => 4, 'some' => 'foursome'),
  1054
+			'five' => array('number' => 5, 'some' => 'fivesome'),
  1055
+			'six' => array('number' => 6, 'some' => 'sixsome')
  1056
+		);
  1057
+		$this->assertEquals($expected, $sorted);
  1058
+
  1059
+		$menus = array(
  1060
+			'blogs' => array('title' => 'Blogs', 'weight' => 3),
  1061
+			'comments' => array('title' => 'Comments', 'weight' => 2),
  1062
+			'users' => array('title' => 'Users', 'weight' => 1),
  1063
+		);
  1064
+		$expected = array(
  1065
+			'users' => array('title' => 'Users', 'weight' => 1),
  1066
+			'comments' => array('title' => 'Comments', 'weight' => 2),
  1067
+			'blogs' => array('title' => 'Blogs', 'weight' => 3),
  1068
+		);
  1069
+		$result = Hash::sort($menus, '{s}.weight', 'ASC');
  1070
+		$this->assertEquals($expected, $result);
  1071
+	}
  1072
+
  1073
+/**
1037 1074
  * Test insert()
1038 1075
  *
1039 1076
  * @return void
13  lib/Cake/Utility/Hash.php
@@ -695,7 +695,8 @@ public static function apply(array $data, $path, $function) {
695 695
  */
696 696
 	public static function sort(array $data, $path, $dir) {
697 697
 		$originalKeys = array_keys($data);
698  
-		if (is_numeric(implode('', $originalKeys))) {
  698
+		$numeric = is_numeric(implode('', $originalKeys));
  699
+		if ($numeric) {
699 700
 			$data = array_values($data);
700 701
 		}
701 702
 		$sortValues = self::extract($data, $path);
@@ -722,7 +723,15 @@ public static function sort(array $data, $path, $dir) {
722 723
 		$keys = array_unique($keys);
723 724
 
724 725
 		foreach ($keys as $k) {
725  
-			$sorted[] = $data[$k];
  726
+			if ($numeric) {
  727
+				$sorted[] = $data[$k];
  728
+				continue;
  729
+			} 
  730
+			if (isset($originalKeys[$k])) {
  731
+				$sorted[$originalKeys[$k]] = $data[$originalKeys[$k]];
  732
+			} else {
  733
+				$sorted[$k] = $data[$k];
  734
+			}
726 735
 		}
727 736
 		return $sorted;
728 737
 	}

0 notes on commit 9015d78

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