Undefined is not a function issue when sorting