replace for() to foreach() from sorter()