Add sorting and filtering in code
I was confused by $view-&gt;setExposedInput saying it accepts $filters as argument, but it actually works for sorting as well. So with that function you can set your exposed filters AND sorts

$view-&gt;setExposedInput([
  'your_filter' =&gt; $your_filter_value, //your exposed filter
  'sort_by' =&gt; 'your_field', //your exposed sort
  'sort_order' =&gt; 'ASC',
]);


Use above code before you execute view.

And make sure you exposed your filters and sorts in your view.