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

Unable to sorting collection #76

Open
przemekgoczal opened this issue Feb 7, 2023 · 0 comments
Open

Unable to sorting collection #76

przemekgoczal opened this issue Feb 7, 2023 · 0 comments

Comments

@przemekgoczal
Copy link

przemekgoczal commented Feb 7, 2023

Hi @pascalbaljet

I'm trying to sort collection in many ways but not result, each time $complaintsSearch returned unsorted data.

Few examples:

$complaints = (new Complaint);

$sortBy = [['complaints_date','desc'],['city','desc']];
            $complaintsSearch = Search::add($complaints, 'company_name')
                ->search('"' . $searchText . '"');
                            $complaintsSearch = $complaintsSearch->toArray();
            usort($complaintsSearch, function($a, $b) {
                return -strcmp($a['complaint_date'], $b['complaint_date']);
            });
            
            $complaintsSearch = collect($complaintsSearch);
            $complaintsSearch = $complaintsSearch->sortByDesc(function ($item) {
                return (string) $item['complaint_date'];
            });
            $complaintsSearch = $complaintsSearch->sortBy(function($complaintsSearch) use ($sortBy) {
                foreach ($sortBy as $sort) {
                    $column = $sort[0];
                    $direction = $sort[1];
                    if ($direction == 'asc') {
                        return $complaintsSearch->$column;
                    } else {
                        return $complaintsSearch->$column;
                    }
                }

And I also trygin sorting data before pass it.

$complaints->orderBy($sortBy);

Each time it returned unsorted collection I don't know why.

Sorting working correctly without use Search

Sorry for misspelled

Thank you.

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