Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add natural, numeric, and string sorting to Hash. Fixes #1700
- Loading branch information
Showing
2 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -717,13 +717,25 @@ public static function apply(array $data, $path, $function) { | ||
/** | /** | ||
* Sorts an array by any value, determined by a Set-compatible path | * Sorts an array by any value, determined by a Set-compatible path | ||
* | * | ||
* ### Sort directions | |||
* | |||
* - `asc` Sort ascending. | |||
* - `desc` Sort descending. | |||
* | |||
* ## Sort types | |||
* | |||
* - `numeric` Sort by numeric value. | |||
* - `regular` Sort by numeric value. | |||
* - `string` Sort by numeric value. | |||
* | |||
* @param array $data An array of data to sort | * @param array $data An array of data to sort | ||
* @param string $path A Set-compatible path to the array value | * @param string $path A Set-compatible path to the array value | ||
* @param string $dir Direction of sorting - either ascending (ASC), or descending (DESC) | * @param string $dir See directions above. | ||
* @param string $type See direction types above. Defaults to 'regular'. | |||
* @return array Sorted array of data | * @return array Sorted array of data | ||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort | * @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort | ||
*/ | */ | ||
public static function sort(array $data, $path, $dir) { | public static function sort(array $data, $path, $dir, $type = 'regular') { | ||
$originalKeys = array_keys($data); | $originalKeys = array_keys($data); | ||
$numeric = is_numeric(implode('', $originalKeys)); | $numeric = is_numeric(implode('', $originalKeys)); | ||
if ($numeric) { | if ($numeric) { | ||
|
@@ -743,12 +755,22 @@ public static function sort(array $data, $path, $dir) { | ||
$values = self::extract($result, '{n}.value'); | $values = self::extract($result, '{n}.value'); | ||
|
|
||
$dir = strtolower($dir); | $dir = strtolower($dir); | ||
$type = strtolower($type); | |||
if ($dir === 'asc') { | if ($dir === 'asc') { | ||
$dir = SORT_ASC; | $dir = SORT_ASC; | ||
} elseif ($dir === 'desc') { | } else { | ||
$dir = SORT_DESC; | $dir = SORT_DESC; | ||
} | } | ||
array_multisort($values, $dir, $keys, $dir); | if ($type === 'numeric') { | ||
$type = SORT_NUMERIC; | |||
} elseif ($type === 'string') { | |||
$type = SORT_STRING; | |||
} elseif ($type === 'natural') { | |||
$type = SORT_NATURAL; | |||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
markstory
Author
Member
|
|||
} else { | |||
$type = SORT_REGULAR; | |||
} | |||
array_multisort($values, $dir, $type, $keys, $dir, $type); | |||
$sorted = array(); | $sorted = array(); | ||
$keys = array_unique($keys); | $keys = array_unique($keys); | ||
|
|
||
|
Use of undefined constant SORT_NATURAL - assumed 'SORT_NATURAL'
This doesnt seem to be a valid sort parameter:
http://php.net/manual/en/function.array-multisort.php
Edit:
seems to be added in 5.4:
http://www.php.net/manual/en/function.sort.php