Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
499f7bfc
Commit
499f7bfc
authored
Jan 08, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ArrayHelper::multisort() test.
parent
c72d7dc9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
3 deletions
+54
-3
ArrayHelper.php
framework/util/ArrayHelper.php
+4
-3
ArrayHelperTest.php
tests/unit/framework/util/ArrayHelperTest.php
+50
-0
No files found.
framework/util/ArrayHelper.php
View file @
499f7bfc
...
...
@@ -243,7 +243,7 @@ class ArrayHelper
* @throws \yii\base\BadParamException if the $ascending or $sortFlag parameters do not have
* correct number of elements as that of $key.
*/
public
static
function
sort
(
&
$array
,
$key
,
$ascending
=
true
,
$sortFlag
=
SORT_REGULAR
)
public
static
function
multi
sort
(
&
$array
,
$key
,
$ascending
=
true
,
$sortFlag
=
SORT_REGULAR
)
{
$keys
=
is_array
(
$key
)
?
$key
:
array
(
$key
);
if
(
empty
(
$keys
)
||
empty
(
$array
))
{
...
...
@@ -273,10 +273,10 @@ class ArrayHelper
}
else
{
$args
[]
=
static
::
getColumn
(
$array
,
$key
);
}
$args
[]
=
$ascending
[
$i
];
$args
[]
=
$ascending
[
$i
]
?
SORT_ASC
:
SORT_DESC
;
$args
[]
=
$flag
;
}
$args
[]
&=
$array
;
$args
[]
=
&
$array
;
call_user_func_array
(
'array_multisort'
,
$args
);
}
}
\ No newline at end of file
tests/unit/framework/util/ArrayHelperTest.php
0 → 100644
View file @
499f7bfc
<?php
namespace
yiiunit\framework\db
;
use
yii\util\ArrayHelper
;
class
ArrayHelperTest
extends
\yii\test\TestCase
{
public
function
testMerge
()
{
}
public
function
testMultisort
()
{
// single key
$array
=
array
(
array
(
'name'
=>
'b'
,
'age'
=>
3
),
array
(
'name'
=>
'a'
,
'age'
=>
1
),
array
(
'name'
=>
'c'
,
'age'
=>
2
),
);
ArrayHelper
::
multisort
(
$array
,
'name'
);
$this
->
assertEquals
(
array
(
'name'
=>
'a'
,
'age'
=>
1
),
$array
[
0
]);
$this
->
assertEquals
(
array
(
'name'
=>
'b'
,
'age'
=>
3
),
$array
[
1
]);
$this
->
assertEquals
(
array
(
'name'
=>
'c'
,
'age'
=>
2
),
$array
[
2
]);
// multiple keys
$array
=
array
(
array
(
'name'
=>
'b'
,
'age'
=>
3
),
array
(
'name'
=>
'a'
,
'age'
=>
2
),
array
(
'name'
=>
'a'
,
'age'
=>
1
),
);
ArrayHelper
::
multisort
(
$array
,
array
(
'name'
,
'age'
));
$this
->
assertEquals
(
array
(
'name'
=>
'a'
,
'age'
=>
1
),
$array
[
0
]);
$this
->
assertEquals
(
array
(
'name'
=>
'a'
,
'age'
=>
2
),
$array
[
1
]);
$this
->
assertEquals
(
array
(
'name'
=>
'b'
,
'age'
=>
3
),
$array
[
2
]);
// case-insensitive
$array
=
array
(
array
(
'name'
=>
'a'
,
'age'
=>
3
),
array
(
'name'
=>
'b'
,
'age'
=>
2
),
array
(
'name'
=>
'A'
,
'age'
=>
1
),
);
ArrayHelper
::
multisort
(
$array
,
array
(
'name'
,
'age'
),
SORT_ASC
,
array
(
SORT_STRING
|
SORT_FLAG_CASE
,
SORT_REGULAR
));
$this
->
assertEquals
(
array
(
'name'
=>
'A'
,
'age'
=>
1
),
$array
[
0
]);
$this
->
assertEquals
(
array
(
'name'
=>
'a'
,
'age'
=>
3
),
$array
[
1
]);
$this
->
assertEquals
(
array
(
'name'
=>
'b'
,
'age'
=>
2
),
$array
[
2
]);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment