Try this for the $search_query code:
Code:
$search_query = "($sql_tbl[products].categoryid='$cat' or $sql_tbl[products].categoryid1='$cat' or $sql_tbl[products].categoryid2='$cat' or $sql_tbl[products].categoryid3='$cat') and $sql_tbl[products].forsale='Y' group by $sql_tbl[products].productid order by $sql_tbl[products].productcode ".($config["General"]["product_order_reversed"]=="Y"?"desc":"asc")." limit $first_page, ".$config["General"]["products_per_page"];
If that doesn't work, look at this thread regarding sorting the products list:
http://forum.x-cart.com/viewtopic.php?t=677
Or this one:
http://forum.x-cart.com/viewtopic.php?t=1277
LOTS of info in there - you just have to extrapolate what you need for your purposes.
hope this helps.