I am using a bespoke module (which has been inherited with the site) and the module sorts the articles shown based on the selected sort order from the XML params:
<field name="sortopt" type="list" default="none" label="Sort Options">
<option value="none">Select a Sort option</option>
<option value="priceasc">Price Ascending</option>
<option value="pricedesc">Price Descending</option>
<option value="titleasc">Title Ascending</option>
<option value="titledesc">Title Descending</option>
<option value="priceascfeatured">Price Ascending - Featured First</option>
<option value="pricedescfeatured">Price Descending - Featured First</option>
</field>
I then create the products array:
$products[] = array('id' => $id, 'title' => $title, 'image' => $image,
'product_url' => $product_url, 'region' => $region,
'featured' => $article->featured, 'product_price' => $product_price);
Then the sort order is:
if($sortopt == 'priceasc') {
array_multisort($sort['product_price'], SORT_NUMERIC, SORT_ASC, $products);
// Sort Price DESC
} elseif($sortopt == 'pricedesc') {
array_multisort($sort['product_price'], SORT_NUMERIC, SORT_DESC, $products);
// Sort Title ASC
} elseif($sortopt == 'titleasc') {
array_multisort($sort['title'], SORT_NUMERIC, SORT_ASC, $products);
// // Sort Title DESC
} elseif($sortopt == 'titledesc') {
array_multisort($sort['title'], SORT_NUMERIC, SORT_DESC, $products);
// Sort region ASC (Default)
} elseif($sortopt == 'none') {
array_multisort($sort['title'], SORT_NUMERIC, SORT_ASC, $products);
}
If an article is set to featured, I need to show this first in all order scenarios. I know I can use array_multisort to sort using featured and price but the following is showing the featured first, but not then putting them into the price sort order.
// Sort Price ASC - Featured First
elseif($sortopt == 'priceascfeatured') {
array_multisort($sort['product_price'], SORT_NUMERIC, SORT_ASC, $products,
$sort['featured'], SORT_NUMERIC, SORT_DESC, $products);
}
// Sort Price DESC - Featured First
elseif($sortopt == 'pricedescfeatured') {
array_multisort($sort['product_price'], SORT_NUMERIC, SORT_DESC, $products,
$sort['featured'], SORT_NUMERIC, SORT_DESC, $products);
}
$sort['region']
for thenone
condition. You might consider this slight change:<option value="regionasc">Select a Sort option</option>
for consistency. Might you (in the future) consider a separate checkbox that offers "Featured First" separate from the select field? or are you happy to leave as is?$sortopt
, it is a perfect candidate for single comparison handling.