refactored sorting filter code