Skip to content

Commit 89f38db

Browse files
committed
Fix #59 (Custom comparer in orderBy is ignored if key selector is cached). Also fixed doc comments.
1 parent 2ef9326 commit 89f38db

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

Tests/Unit/EnumerableTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,9 @@ function testOrderBy()
813813
$this->assertEnumValuesSame(
814814
[ 2, 3, 9, 4 ],
815815
E::from([ 999 => 9, 2 => 2, 33 => 3, 4444 => 4 ])->orderBy('$k', $compareLen));
816+
$this->assertEnumValuesSame(
817+
[ 2, 3, 9, 4 ],
818+
E::from([ 'aaa' => 9, 'b' => 2, 'cc' => 3, 'dddd' => 4 ])->orderBy('$k', $compareLen));
816819

817820
// both keys and values sorted
818821
$this->assertEnumOrderSame(

YaLinqo/Enumerable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function where($predicate): Enumerable
213213
* <p>Three methods are defined to extend the type {@link OrderedEnumerable}, which is the return type of this method. These three methods, namely {@link OrderedEnumerable::thenBy thenBy}, {@link OrderedEnumerable::thenByDescending thenByDescending} and {@link OrderedEnumerable::thenByDir thenByDir}, enable you to specify additional sort criteria to sort a sequence. These methods also return an OrderedEnumerable, which means any number of consecutive calls to thenBy, thenByDescending or thenByDir can be made.
214214
* <p>Because OrderedEnumerable inherits from Enumerable, you can call {@link orderBy}, {@link orderByDescending} or {@link orderByDir} on the results of a call to orderBy, orderByDescending, orderByDir, thenBy, thenByDescending or thenByDir. Doing this introduces a new primary ordering that ignores the previously established ordering.
215215
* <p>This method performs an unstable sort; that is, if the keys of two elements are equal, the order of the elements is not preserved. In contrast, a stable sort preserves the order of elements that have the same key. Internally, {@link usort} is used.
216-
* @param int|bool $sortOrder A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value).
216+
* @param int|bool $sortOrder A direction in which to order the elements: false or SORT_ASC for ascending (by increasing value), true or SORT_DESC for descending (by decreasing value).
217217
* @param callable|string|null $keySelector {(v, k) ==> key} A function to extract a key from an element. Default: value.
218218
* @param callable|string|int|null $comparer {(a, b) ==> diff} Difference between a and b: &lt;0 if a&lt;b; 0 if a==b; &gt;0 if a&gt;b. Can also be a combination of SORT_ flags.
219219
* @return OrderedEnumerable

YaLinqo/OrderedEnumerable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class OrderedEnumerable extends Enumerable
3333
/**
3434
* @internal
3535
* @param Enumerable $source
36-
* @param int|bool $sortOrder A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value).
36+
* @param int|bool $sortOrder A direction in which to order the elements: false or SORT_ASC for ascending (by increasing value), true or SORT_DESC for descending (by decreasing value).
3737
* @param int $sortFlags Sort flags for array_multisort.
3838
* @param bool $isReversed Whether comparer result needs to be negated (used in usort).
3939
* @param callable $keySelector {(v, k) ==> key} A function to extract a key from an element.
@@ -65,7 +65,7 @@ private function getSingleComparer()
6565
* <p>Three methods are defined to extend the type OrderedEnumerable, which is the return type of this method. These three methods, namely {@link thenBy}, {@link thenByDescending} and {@link thenByDir}, enable you to specify additional sort criteria to sort a sequence. These methods also return an OrderedEnumerable, which means any number of consecutive calls to thenBy, thenByDescending or thenByDir can be made.
6666
* <p>Because OrderedEnumerable inherits from {@link Enumerable}, you can call {@link Enumerable::orderBy orderBy}, {@link Enumerable::orderByDescending orderByDescending} or {@link Enumerable::orderByDir orderByDir} on the results of a call to orderBy, orderByDescending, orderByDir, thenBy, thenByDescending or thenByDir. Doing this introduces a new primary ordering that ignores the previously established ordering.
6767
* <p>This method performs an unstable sort; that is, if the keys of two elements are equal, the order of the elements is not preserved. In contrast, a stable sort preserves the order of elements that have the same key. Internally, {@link usort} is used.
68-
* @param int|bool $sortOrder A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value).
68+
* @param int|bool $sortOrder A direction in which to order the elements: false or SORT_ASC for ascending (by increasing value), true or SORT_DESC for descending (by decreasing value).
6969
* @param callable|string|null $keySelector {(v, k) ==> key} A function to extract a key from an element. Default: value.
7070
* @param callable|string|int|null $comparer {(a, b) ==> diff} Difference between a and b: &lt;0 if a&lt;b; 0 if a==b; &gt;0 if a&gt;b. Can also be a combination of SORT_ flags.
7171
* @return \YaLinqo\OrderedEnumerable
@@ -136,7 +136,7 @@ private function trySortBySingleField($array, bool $canMultisort)
136136
arsort($array, $this->sortFlags);
137137
}
138138
elseif ($this->keySelector === Functions::$key) {
139-
if ($canMultisort)
139+
if (!$canMultisort)
140140
uksort($array, $this->getSingleComparer());
141141
elseif ($this->sortOrder == SORT_ASC)
142142
ksort($array, $this->sortFlags);

YaLinqo/Utils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class Utils
2626
private static $compareFunctionToSortFlags = [
2727
null => SORT_REGULAR,
2828
'strcmp' => SORT_STRING,
29-
'strcasecmp' => 10 /*SORT_STRING | SORT_FLAG_CASE*/,
29+
'strcasecmp' => SORT_STRING | SORT_FLAG_CASE,
3030
'strcoll' => SORT_LOCALE_STRING,
3131
'strnatcmp' => SORT_NATURAL,
32-
'strnatcasecmp' => 14 /*SORT_NATURAL | SORT_FLAG_CASE*/,
32+
'strnatcasecmp' => SORT_NATURAL | SORT_FLAG_CASE,
3333
];
3434

3535
/**

readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# *YaLinqo: Yet Another LINQ to Objects for PHP*
22

3-
[![Travis CI Status](https://img.shields.io/travis/Athari/YaLinqo.svg)](https://travis-ci.org/Athari/YaLinqo)
43
[![Coveralls Coverage](https://img.shields.io/coveralls/Athari/YaLinqo/master.svg)](https://coveralls.io/r/Athari/YaLinqo)
54
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/Athari/YaLinqo.svg)](https://scrutinizer-ci.com/g/Athari/YaLinqo)
65
[![Packagist Downloads](https://img.shields.io/packagist/dt/athari/yalinqo.svg)](https://packagist.org/packages/athari/yalinqo)

0 commit comments

Comments
 (0)