Added sorting to database queries