Create a generic method for sorting and trimming