added sorting functionality when retrieving from db