Browse Source

Simplified ArrayHelper::multisort().

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
53457a0a3f
  1. 18
      framework/yii/helpers/BaseArrayHelper.php
  2. 4
      tests/unit/framework/helpers/ArrayHelperTest.php

18
framework/yii/helpers/BaseArrayHelper.php

@ -339,13 +339,10 @@ class BaseArrayHelper
* `SORT_REGULAR`, `SORT_NUMERIC`, `SORT_STRING`, `SORT_LOCALE_STRING`, `SORT_NATURAL` and `SORT_FLAG_CASE`. * `SORT_REGULAR`, `SORT_NUMERIC`, `SORT_STRING`, `SORT_LOCALE_STRING`, `SORT_NATURAL` and `SORT_FLAG_CASE`.
* Please refer to [PHP manual](http://php.net/manual/en/function.sort.php) * Please refer to [PHP manual](http://php.net/manual/en/function.sort.php)
* for more details. When sorting by multiple keys with different sort flags, use an array of sort flags. * for more details. When sorting by multiple keys with different sort flags, use an array of sort flags.
* @param boolean|array $caseSensitive whether to sort string in case-sensitive manner. This parameter
* is used only when `$sortFlag` is `SORT_STRING`.
* When sorting by multiple keys with different case sensitivities, use an array of boolean values.
* @throws InvalidParamException if the $descending or $sortFlag parameters do not have * @throws InvalidParamException if the $descending or $sortFlag parameters do not have
* correct number of elements as that of $key. * correct number of elements as that of $key.
*/ */
public static function multisort(&$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR, $caseSensitive = true) public static function multisort(&$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR)
{ {
$keys = is_array($key) ? $key : [$key]; $keys = is_array($key) ? $key : [$key];
if (empty($keys) || empty($array)) { if (empty($keys) || empty($array)) {
@ -362,21 +359,10 @@ class BaseArrayHelper
} elseif (count($sortFlag) !== $n) { } elseif (count($sortFlag) !== $n) {
throw new InvalidParamException('The length of $sortFlag parameter must be the same as that of $keys.'); throw new InvalidParamException('The length of $sortFlag parameter must be the same as that of $keys.');
} }
if (is_scalar($caseSensitive)) {
$caseSensitive = array_fill(0, $n, $caseSensitive);
} elseif (count($caseSensitive) !== $n) {
throw new InvalidParamException('The length of $caseSensitive parameter must be the same as that of $keys.');
}
$args = []; $args = [];
foreach ($keys as $i => $key) { foreach ($keys as $i => $key) {
$flag = $sortFlag[$i]; $flag = $sortFlag[$i];
$cs = $caseSensitive[$i]; $args[] = static::getColumn($array, $key);
if (!$cs && ($flag === SORT_STRING)) {
$flag = $flag | SORT_FLAG_CASE;
$args[] = static::getColumn($array, $key);
} else {
$args[] = static::getColumn($array, $key);
}
$args[] = $direction[$i]; $args[] = $direction[$i];
$args[] = $flag; $args[] = $flag;
} }

4
tests/unit/framework/helpers/ArrayHelperTest.php

@ -129,13 +129,13 @@ class ArrayHelperTest extends TestCase
['name' => 'A', 'age' => 1], ['name' => 'A', 'age' => 1],
]; ];
ArrayHelper::multisort($array, ['name', 'age'], false, [SORT_STRING, SORT_REGULAR]); ArrayHelper::multisort($array, ['name', 'age'], SORT_ASC, [SORT_STRING, SORT_REGULAR]);
$this->assertEquals(['name' => 'A', 'age' => 1], $array[0]); $this->assertEquals(['name' => 'A', 'age' => 1], $array[0]);
$this->assertEquals(['name' => 'B', 'age' => 4], $array[1]); $this->assertEquals(['name' => 'B', 'age' => 4], $array[1]);
$this->assertEquals(['name' => 'a', 'age' => 3], $array[2]); $this->assertEquals(['name' => 'a', 'age' => 3], $array[2]);
$this->assertEquals(['name' => 'b', 'age' => 2], $array[3]); $this->assertEquals(['name' => 'b', 'age' => 2], $array[3]);
ArrayHelper::multisort($array, ['name', 'age'], false, [SORT_STRING, SORT_REGULAR], false); ArrayHelper::multisort($array, ['name', 'age'], SORT_ASC, [SORT_STRING | SORT_FLAG_CASE, SORT_REGULAR]);
$this->assertEquals(['name' => 'A', 'age' => 1], $array[0]); $this->assertEquals(['name' => 'A', 'age' => 1], $array[0]);
$this->assertEquals(['name' => 'a', 'age' => 3], $array[1]); $this->assertEquals(['name' => 'a', 'age' => 3], $array[1]);
$this->assertEquals(['name' => 'b', 'age' => 2], $array[2]); $this->assertEquals(['name' => 'b', 'age' => 2], $array[2]);

Loading…
Cancel
Save