diff --git a/cake/libs/set.php b/cake/libs/set.php index 6d9e50043..d8501030e 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -1003,14 +1003,13 @@ class Set extends Object { function __flatten($results, $key = null) { $stack = array(); foreach ($results as $k => $r) { + $id = $k; + if (!is_null($key)) { + $id = $key; + } if (is_array($r)) { - $stack = array_merge($stack, Set::__flatten($r, $k)); + $stack = array_merge($stack, Set::__flatten($r, $id)); } else { - if (!empty($key)) { - $id = $key; - } else { - $id = $k; - } $stack[] = array('id' => $id, 'value' => $r); } } @@ -1035,7 +1034,9 @@ class Set extends Object { } array_multisort($values, $dir, $keys, $dir); $sorted = array(); - + + $keys = array_unique($keys); + foreach ($keys as $k) { $sorted[] = $data[$k]; } diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 85cc00fcb..0ea51a3d8 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -221,6 +221,41 @@ class SetTest extends UnitTestCase { ); $a = Set::sort($a, '{n}.Person.name', 'asc'); $this->assertIdentical($a, $b); + + + // given a path to an array as the sort path, sort by lowest or highest value in array + $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 = Set::sort($a, '{n}.{n}', 'asc'); + $this->assertIdentical($a, $b); + + + // as per above, but nested arrays + $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 = Set::sort($a, '{n}', 'asc'); + $this->assertIdentical($a, $b); + // if every element doesn't have the matching key, the one without is compared as empty $a = array(