Wrong sorting - please help me debug this