Jump to content
Sign in to follow this  
way1000

sort words in a text file

Recommended Posts

way1000

i have a text file that contains words that repeat randomly, i have to sort the words by order of some sort

the text file looks like this:

why 
when 
why 
when 
why 
when 
why 
when 
why 
when 
why 
when 
why 
when 
why 
when 
why 
when 
why 
when 
why 
when 
why 
when

i have to sort the words in this order (still repetitive)

why
why
when
why
why

why
why
when
why
why

why
why
when
why
why

why
why
when
why
why

i attached the text file in which the words have t be sorted

 

thanks a lot in advance

 

WordList1.txt

Edited by way1000

Share this post


Link to post
Share on other sites
spudw2k

Well, for one thing it looks like you are not sorting, but parsing, otherwise you'd have the same amount of output lines, but you are obviously omitting "when"s from the output.  It looks more like a pattern recognition of some sorts, but without knowing what the end goal is it may only be tailored for this single scenario.

For example, what happens if you use other words for input?  What if the input words aren't laid out in an alternating fashion?  How do you expect the last when to be handled?...ignored?

I'm sorry, but it is just not clear to me what pattern you are trying to produce.  In simplest terms, for this input example you could use the following logic:

read first line in text file
print read line twice
read next line in text file
print read line once
read next line in text file
print read line twice
print newline/crlf

repeat until end of file.

of course to deal with the EOF you need some sort of check for that if you want "good" output.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×