Fixed template sorting bug

Removed casting as integers to support 3.x API.