The problem is Virtuemart code.
The problem was they were sending unescaped code to the database.
I added the following line in /administrator/components/com_virtuemart/models/product.php on line 1575:
$orderByValue = addslashes($orderByValue);
This is a very crude way of escaping data, but at least it is much more secure, and will not be rejected by the database. The system works with this workaround. So hopefully it helps anyone with a similar problem.