refactor is_sorted func