referenced wrong variable in sorted function call