Move sort function to view