Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing Set::sort() for arrays with out of sequence, or missing keys.

Tests added.
Fixes #1312
  • Loading branch information...
commit 7d158e8d1f8ea6a156e169c021b831740d8ca838 1 parent 4214242
@markstory markstory authored
Showing with 31 additions and 1 deletion.
  1. +4 −1 cake/libs/set.php
  2. +27 −0 cake/tests/cases/libs/set.test.php
View
5 cake/libs/set.php
@@ -1092,6 +1092,10 @@ function __flatten($results, $key = null) {
* @static
*/
function sort($data, $path, $dir) {
+ $originalKeys = array_keys($data);
+ if (is_numeric(implode('', $originalKeys))) {
+ $data = array_values($data);
+ }
$result = Set::__flatten(Set::extract($data, $path));
list($keys, $values) = array(Set::extract($result, '{n}.id'), Set::extract($result, '{n}.value'));
@@ -1103,7 +1107,6 @@ function sort($data, $path, $dir) {
}
array_multisort($values, $dir, $keys, $dir);
$sorted = array();
-
$keys = array_unique($keys);
foreach ($keys as $k) {
View
27 cake/tests/cases/libs/set.test.php
@@ -355,6 +355,33 @@ function testSort() {
}
/**
+ * test sorting with out of order keys.
+ *
+ * @return void
+ */
+ function testSortWithOutOfOrderKeys() {
+ $data = array(
+ 9 => array('class' => 510, 'test2' => 2),
+ 1 => array('class' => 500, 'test2' => 1),
+ 2 => array('class' => 600, 'test2' => 2),
+ 5 => array('class' => 625, 'test2' => 4),
+ 0 => array('class' => 605, 'test2' => 3),
+ );
+ $expected = array(
+ array('class' => 500, 'test2' => 1),
+ array('class' => 510, 'test2' => 2),
+ array('class' => 600, 'test2' => 2),
+ array('class' => 605, 'test2' => 3),
+ array('class' => 625, 'test2' => 4),
+ );
+ $result = Set::sort($data, '{n}.class', 'asc');
+ $this->assertEqual($expected, $result);
+
+ $result = Set::sort($data, '{n}.test2', 'asc');
+ $this->assertEqual($expected, $result);
+ }
+
+/**
* testExtract method
*
* @access public
Please sign in to comment.
Something went wrong with that request. Please try again.