refactor sort event functions to use a generic function