Random Sort

hello,
I would like to create something that can change the sort order of a .txt file that I have.
lets say I have something like this in mp3.lst

artist 1 - track 1
artist 1 - track 2
artist 2 - track 1
artist 2 - track 2

But I want in different order each line. I got around 600 lines. What can I use for that?
LVL 2
CyberPunksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

r-kCommented:
Just create an array of random numbers between 1 and 600, and use that to "index" your file. For example, if the array of numbers (assuming above example of 4 lines) contains:

 2
 4
 1
 3

then you would play the song on line 2 first, the song on line 4 second, and so on.

Next time, create a new random array, e.g.

 3
 2
 4
 1

then you play in that order.

Just extend this concept to 600 instead of 4.
r-kCommented:
The above example does not need the file to be re-written in a new order. If you also wanted to write the file out in a new order, then just read all records in the file into memory, and write back to a new file in the order of the random number array.
InteractiveMindCommented:
> "But I want in different order each line."
Do you mean a random order? If so, r-k has solved it..
Otherwise, can you specify what order you want it to be in please?
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

ozoCommented:
perl -ne 'push @a,$_;$j=rand $.;@a[$j,-1]=@a[-1,$j];END{print @a}' mp3.lst

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
guitaristxCommented:
The problem with r-k's solution is that you're not guaranteed to have every song played (also meaning that you could have the same song play twice in your "shuffled" list).  A better solution would be to create a list/array/whatever of integers, 0 to 599, and iterate through the list.  You swap the current integer with the integer at a randomly chosen index **greater than the current index**.  You then use this shuffled list to index your original list.

This is a *true* shuffle.  You can use this to programmatically shuffle a deck of cards for a card game, for example.
nayernaguibCommented:
What programming language are you using?

_______________

  Nayer Naguib
CyberPunksAuthor Commented:
ozo your solution worked fine thanks, but it wont save the info to the file. is there anyway to open the mp3.lst and save it in mp3ds.lst ? if not is fine.
ozoCommented:
perl -ne 'push @a,$_;$j=rand $.;@a[$j,-1]=@a[-1,$j];END{print @a}' mp3.lst > mp3ds.lst
CyberPunksAuthor Commented:
thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.