Add function to sort images