Ok. Got the issue. Any of the sorting functions in PHP is not stable which means that if two elements compare equal their position is undefined, not remained. Can't find a quick solution to maintaining item ordering in such case.