Array sorting bug fixing