use reentrant qsort_r where available

- this avoids one TLS access per comparison
use inner function to avoid the 'cmp' mangling