Sorting with iterations and looking into directories