Sort implementation - first order functions