modified functions using template for more general sorting algorithm