I'm looking for pseudocode suggestions for sorting my mp3 files in a way that avoids title and artist repetition. I listen to crooners - Frank Sinatra, Tony Bennett, Ella Fitzgerald etc. singing old standards. Each artist records many of the same songs - Fly Me To The Moon, The Way You Look Tonight, Stardust etc. My goal is to arrange the songs (or order the playlist) with the maximum space between artists and song titles. So if I have 2000 songs and 20 are by Ella I'd like to hear her only once in every 100 songs. If 10 artists sing Fly Me To The Moon I'd like to hear it once in every 200 songs.

I know this is a fairly wide open question. I haven't started programming it yet so I'm just looking for suggestions of a good approach to take. I actually have some other requirements regarding evenly spacing other song attributes but I won't get into that here.

  • 1
    Sorting is probably the wrong thing to search for in this case. Sorting algorithms try to put similar items as close together as possible, while you want them far apart. Would it be a problem if your playlist becomes fairly predictable (After a Frank Sinatra song you usually get a Ella Fitzgerald song, etc.)? Sep 16 '17 at 14:46
  • "maximum space between artists and song titles" can you elaborate more on that? Maximum minimum distance between any pair of songs with either same artist or title ("closest pair")? Sum (or mean) of distances for each song (sum /mean of "nearest neighbor")? Sum/mean of distances between songs per artist / title (sum/mean of "closest pair" within artist/title subsets)? Another metric? Sep 17 '17 at 14:02

How serious are you about "maximum space between"?

Consider a double shuffle. Shuffle the artists and shuffle the titles. The only thing complicating this is that not every artist has sung every title. So what? Shuffle them as if they have and ignore the ones that don't exist. The problem with that is the songs that don't exist were supposed to keep the ones that do seperated. If that's not good enough we have to be aware of both artist and title as we select.

If you insist on a more reliable method then arbitrarily pick one dimension (artist or title) to randomly select without replacement first. Let's say you pick artist to go first. Create a list of all artists. Then a list of all titles.

Randomly choose, and remove, an artist only if a title by that artist exists in the title list. If so also remove the title from the title list.

Eventually this will exhaust the artist list. Once it does populate it again but don't repopulate the title list until it's exhausted.

If you want to be sure everything is played once before anything is played again you need to also keep a song list, remove from it, and only repopulate the other lists from what is still in the song list.

Doing this can't guarantee the maximum space because you might get a better arrangement by selecting title before artist. Whichever one you pick to choose first has it's distance maximized while the other might not end up maximized. Again, the fact that not every artist has sung every song is impacting the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.