correctly implements array sorting