Fix a bug with sorting of array