Refactor lines in table sort helper