laravel-cross-eloquent-search icon indicating copy to clipboard operation
laravel-cross-eloquent-search copied to clipboard

Unable to sorting collection

Open przemekgoczal opened this issue 2 years ago • 0 comments

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.

przemekgoczal avatar Feb 07 '23 20:02 przemekgoczal