Skip to content

Commit

Permalink
Fix sorting empty data with Hash & Set.
Browse files Browse the repository at this point in the history
Fixes #3420
  • Loading branch information
markstory committed Nov 30, 2012
1 parent f250592 commit 889c1eb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Cake/Test/Case/Utility/HashTest.php
Expand Up @@ -928,6 +928,9 @@ public function testExtractUnevenKeys() {
* @return void
*/
public function testSort() {
$result = Hash::sort(array(), '{n}.name', 'asc');
$this->assertEquals(array(), $result);

$a = array(
0 => array(
'Person' => array('name' => 'Jeff'),
Expand Down
3 changes: 3 additions & 0 deletions lib/Cake/Test/Case/Utility/SetTest.php
Expand Up @@ -225,6 +225,9 @@ public function testMerge() {
* @return void
*/
public function testSort() {
$result = Set::sort(array(), '{n}.name', 'asc');
$this->assertEquals(array(), $result);

$a = array(
0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
Expand Down
3 changes: 3 additions & 0 deletions lib/Cake/Utility/Hash.php
Expand Up @@ -730,6 +730,9 @@ public static function apply(array $data, $path, $function) {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort
*/
public static function sort(array $data, $path, $dir, $type = 'regular') {
if (empty($data)) {
return array();
}
$originalKeys = array_keys($data);
$numeric = is_numeric(implode('', $originalKeys));
if ($numeric) {
Expand Down
3 changes: 3 additions & 0 deletions lib/Cake/Utility/Set.php
Expand Up @@ -956,6 +956,9 @@ protected static function _flatten($results, $key = null) {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::sort
*/
public static function sort($data, $path, $dir) {
if (empty($data)) {
return $data;
}
$originalKeys = array_keys($data);
$numeric = false;
if (is_numeric(implode('', $originalKeys))) {
Expand Down

2 comments on commit 889c1eb

@steffann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick solution!

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome 👍

Please sign in to comment.