Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom comparer in orderBy is ignored if key selector is cached #59

Open
dima-stefantsov opened this issue Feb 25, 2022 · 0 comments
Open
Assignees
Labels

Comments

@dima-stefantsov
Copy link

Using latest "composer require athari/yalinqo", v2.4.2

<?php

require 'vendor/autoload.php';

$qwe = [
    'key3' => 3,
    'key2' => 1,
    'key1' => 2
];

$asd =
    from($qwe)->
    orderBy(
        // #1
        function($v, $k) {
            return $k;
        },

        // #2
        // '$k',

        // #3
        // YaLinqo\Functions::$key,
    function($a, $b) {
        echo "in\n";
        return $b <=> $a;
    })->
    toArray();

var_dump($asd);

Hi Alexander. If I run code like shown, with key_selector#1, I can see "echo in\n" executed, like expected.
But If I run code with key_selector#2 or key_selector#3, I never see "echo in\n" executed, $comparer is never executed. Sorting still works, but not with my custom comparer, just by alphabet.

I've digged into the sources, and I've found vendor\athari\yalinqo\YaLinqo\OrderedEnumerable.php:127

trySortBySingleField:
  elseif ($this->keySelector === Functions::$key)
    elseif ($this->sortOrder == SORT_ASC) {
      ksort($array, $this->sortFlags);

If your code found current key selector function is the one from the cache, it never even tries to run provided custom comparer function.

I expect orderBy to always run comparer function if it is present.
Thank you.

@Athari Athari self-assigned this Feb 25, 2022
@Athari Athari added the type:bug Bug label Feb 25, 2022
@Athari Athari changed the title orderBy bug sorting by key Custom comparer in orderBy is ignored if key selector is cached Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants