Skip to content
Permalink
Browse files

Ability for Hash::sort to sort case-insensitively

  • Loading branch information...
Adrian Gunawan
Adrian Gunawan committed Aug 12, 2015
1 parent 6fae3f6 commit a217556c13ab57ca15df749ae4da02bf7ac4379e
Showing with 99 additions and 5 deletions.
  1. +82 −1 lib/Cake/Test/Case/Utility/HashTest.php
  2. +17 −4 lib/Cake/Utility/Hash.php
@@ -1301,6 +1301,43 @@ public function testSortNatural() {
$this->assertEquals($expected, $result);
}
/**
* Test natural sorting ignoring case.
*
* @return void
*/
public function testSortNaturalIgnoreCase() {
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('SORT_NATURAL is available since PHP 5.4.');
}
$items = array(
array('Item' => array('image' => 'img1.jpg')),
array('Item' => array('image' => 'img99.jpg')),
array('Item' => array('image' => 'Img12.jpg')),
array('Item' => array('image' => 'Img10.jpg')),
array('Item' => array('image' => 'img2.jpg')),
);
$result = Hash::sort($items, '{n}.Item.image', 'desc', 'natural', true);
$expected = array(
array('Item' => array('image' => 'img99.jpg')),
array('Item' => array('image' => 'Img12.jpg')),
array('Item' => array('image' => 'Img10.jpg')),
array('Item' => array('image' => 'img2.jpg')),
array('Item' => array('image' => 'img1.jpg')),
);
$this->assertEquals($expected, $result);
$result = Hash::sort($items, '{n}.Item.image', 'asc', 'natural', true);
$expected = array(
array('Item' => array('image' => 'img1.jpg')),
array('Item' => array('image' => 'img2.jpg')),
array('Item' => array('image' => 'Img10.jpg')),
array('Item' => array('image' => 'Img12.jpg')),
array('Item' => array('image' => 'img99.jpg')),
);
$this->assertEquals($expected, $result);
}
/**
* Test that sort() with 'natural' type will fallback to 'regular' as SORT_NATURAL is introduced in PHP 5.4
*
@@ -1355,7 +1392,7 @@ public function testSortWithOutOfOrderKeys() {
*
* @return void
*/
public function testSortString() {
public function testSortStringKeys() {
$toSort = array(
'four' => array('number' => 4, 'some' => 'foursome'),
'six' => array('number' => 6, 'some' => 'sixsome'),
@@ -1387,6 +1424,50 @@ public function testSortString() {
$this->assertEquals($expected, $result);
}
/**
* test sorting with string ignoring case.
*
* @return void
*/
public function testSortStringIgnoreCase() {
$toSort = array(
array('Item' => array('name' => 'bar')),
array('Item' => array('name' => 'Baby')),
array('Item' => array('name' => 'Baz')),
array('Item' => array('name' => 'bat')),
);
$sorted = Hash::sort($toSort, '{n}.Item.name', 'asc', 'string', true);
$expected = array(
array('Item' => array('name' => 'Baby')),
array('Item' => array('name' => 'bar')),
array('Item' => array('name' => 'bat')),
array('Item' => array('name' => 'Baz')),
);
$this->assertEquals($expected, $sorted);
}
/**
* test regular sorting ignoring case.
*
* @return void
*/
public function testSortRegularIgnoreCase() {
$toSort = array(
array('Item' => array('name' => 'bar')),
array('Item' => array('name' => 'Baby')),
array('Item' => array('name' => 'Baz')),
array('Item' => array('name' => 'bat')),
);
$sorted = Hash::sort($toSort, '{n}.Item.name', 'asc', 'regular', true);
$expected = array(
array('Item' => array('name' => 'Baby')),
array('Item' => array('name' => 'bar')),
array('Item' => array('name' => 'bat')),
array('Item' => array('name' => 'Baz')),
);
$this->assertEquals($expected, $sorted);
}
/**
* Test insert()
*
@@ -843,10 +843,11 @@ public static function apply(array $data, $path, $function) {
* @param string $path A Set-compatible path to the array value
* @param string $dir See directions above. Defaults to 'asc'.
* @param string $type See direction types above. Defaults to 'regular'.
* @param string $ignoreCase Case insensitive sorting. Defaults to false.
* @return array Sorted array of data
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort
*/
public static function sort(array $data, $path, $dir = 'asc', $type = 'regular') {
public static function sort(array $data, $path, $dir = 'asc', $type = 'regular', $ignoreCase = false) {
if (empty($data)) {
return array();
}
@@ -870,9 +871,16 @@ public static function sort(array $data, $path, $dir = 'asc', $type = 'regular')
$dir = strtolower($dir);
$type = strtolower($type);
if ($type === 'natural' && version_compare(PHP_VERSION, '5.4.0', '<')) {
$type = 'regular';
// Natural and case insensitive sort is only supported from >= 5.4.0
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
if ($type === 'natural' || $type === 'natural_ignore_case' || $type === 'regular_ignore_case') {
$type = 'regular';
} elseif ($type == 'string_ignore_case') {
$type = 'string';
}
}
if ($dir === 'asc') {
$dir = SORT_ASC;
} else {
@@ -887,7 +895,12 @@ public static function sort(array $data, $path, $dir = 'asc', $type = 'regular')
} else {
$type = SORT_REGULAR;
}
array_multisort($values, $dir, $type, $keys, $dir, $type);
if ($ignoreCase) {
$values = array_map('strtolower', $values);
}
array_multisort($values, $dir, $type, $keys, $dir);
$sorted = array();
$keys = array_unique($keys);

0 comments on commit a217556

Please sign in to comment.
You can’t perform that action at this time.