Does $categories contain all images? (This should be no, or else you should have duplicates)
You need to look at the function that generates $categories, it's got nothing to do with this foreach. But you are on the right track. You can possible add DESC in your SQL, or similar. Depending on how the SQL looks and the database design.