Moved range and sort code so that it also works for predefined views.