Pulls from database and sorts accordingly.