Solving problem with sorting