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

generic chain sort #9

Open
webbby opened this issue Jan 13, 2021 · 0 comments
Open

generic chain sort #9

webbby opened this issue Jan 13, 2021 · 0 comments

Comments

@webbby
Copy link

webbby commented Jan 13, 2021

Good job man php is terrible with sorting.
Maybe you should add some general generic method for sorting.
I had to extend your class for ordering collection of objects with:

class Sort extends \Tuck\Sort\Sort {
    const ASC = "asc";
    const DESC = "desc";

    static function chainGeneric(array $keysOrder = []) : SortChain {
        $sortChain = self::chain();
        foreach ($keysOrder as $key => $order) {
            if ($order == self::ASC) {
                $sortChain = $sortChain->asc(function ($item) use ($key) {
                    return $item[$key] ?? null;
                });
            } else if ($order == self::DESC) {
                $sortChain = $sortChain->desc(function ($item) use ($key) {
                    return $item[$key] ?? null;
                });
            }
        }

        return $sortChain;
    }
}

invoke

$sorter = Sort::chainGeneric([ 'apples' => 'asc', 'lemons' => 'desc']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant