changed doRecursion to compareAndSort