Back to SiteSearch Support

octobercms30225
octobercms30225

How is it possible to sort the results by date?

OFFLINE
gavinfostered11132
gavinfostered11132

An example of sorting ascending/descending based on user input. Hopefully help someone in the future :)

function onEnd() {
    // Get order paramater
    $order = Input::get('order');

    // Set results collection from query
    $resultsCollection = $this->page->components['searchResults']->resultCollection;

    // Set sort Ascending (saleprice)
    $sortAsc = $resultsCollection->sortBy(function($result) {
        return $result->meta['saleprice'];
    });

    // Set sort Descending (saleprice)
    $sortDesc = $resultsCollection->sortByDesc(function($result) {
        return $result->meta['saleprice'];
    });

    // Overwrite the resultsCollection based on order parameter
    switch ($order) {
        case 'price-high-low':
            $this->page->components['searchResults']->resultCollection = $sortDesc;
            break;
        case 'price-low-high':
            $this->page->components['searchResults']->resultCollection = $sortAsc;
            break;
        default:
            $this->page->components['searchResults']->resultCollection = $sortDesc;
            break;
    }    
}

1-3 of 3