Fix incorrect sorting by rank in TV grid on template create/update screen

Fix incorrect sorting by rank in TV grid on template create/update screen - the IF() statements in the underlying query were forcing the sort to use alphanumeric sorting as opposed to natural sorting.

Fixes #6958.