restored foreach in sort