With an array like this:
[google, google, yahoo, yahoo, yahoo, msn, msn, msn, google]
what’s the best way to arrange it with as few adjacent values as possible?
The goal is to get something like this, for instance:
[google, msn, yahoo, google, yahoo, msn, yahoo, msn, google]
Even better would be an algoritnm that ensures that any similar values are as far as possible from each other.
And even better would be to do it directly in SQL.
I’m doing that for a script I’m working on that sends newsletters, and I’d like to avoid sending too many emails to the same domain at once.
[edit] I’m using MySQL
Using
SQL Server 2005and up, you might take advantage of theROW_NUMBERfunctionalityYour requirement that they should be as far away from each other as possible might not be met but all in all, this would suffice.
SQL Statement
Results
Test script