Template-based qsort