Page 1 of 1
alphabetize lists
Posted: Sun May 26, 2002 2:21 pm
by Guest
How would you alphabetize a list from a .txt file?
PLEASE RESPOND SOON!!!!!!!!!!!
Re: Alpebatizing lists
Posted: Mon Jun 24, 2002 8:16 am
by Guest
i have no idea, but if you find out, can you email me? thanx
Re: Alpebatizing lists
Posted: Fri Nov 01, 2002 10:58 pm
by Jehovah
I know how to do it, but class is almost over, will respond tomarow at 11:30 AM
Re: Alpebatizing lists
Posted: Fri Nov 01, 2002 11:00 pm
by Jehovah
correction, monday at 11:30 AM
Re: Alpebatizing lists
Posted: Mon Nov 04, 2002 12:45 pm
by Jehovah
'Please note: this is most likely not the best way to do 'it, I have never done this until now... but providing this 'is not in a tite loop, it goes wrather fast. [This was 'writen in qb4.5]
'You are free to change/destribute this file without giving me credit.
'all it does is load the file into a matrix, then go threw 'and see if the last line's first is less, if it is, then it swaps 'the two. the enforce loop is to make sure everything 'goes smoothly, but during testing, only moved blank 'spaces to the end of the file (top or bottom, don't 'know). as it is, it is not case sensitive. if you want it to 'be, you cannot just remove the lcase$ on all of the if 'statements. this is because ALL of the upper case 'alphabet comes before lower on the ascii table. to 'make it be case sensitiv, you must make it so that 'when it encounters 2 like values, it finds wich is less 'without the lcase$, if they are the same, don't swap, if 'one is then swap in the respective direction. this 'program was only tested on small lists since I am lazy 'and did not want to spend time on testing 7 minuts
'worth of code (that's 'bout how long it took). I will 'admit, this took a little more thought then I expected it 'too since I have never done it. But it was still wrather 'easy. If you need more help on the ascii table or I/O
'feel free to ask since I use both quit often in my 'programs.
'$DYNAMIC
DIM file(1) AS STRING 'can change to be dynamic by using '$' instead of as clause
'$STATIC
file$ = "C:\windows\desktop\test.txt"
OPEN file$ FOR INPUT AS 1
DO
LINE INPUT #1, test$
lines = lines + 1
LOOP WHILE NOT EOF(1)
CLOSE 1
OPEN file$ FOR INPUT AS 1
REDIM file(lines)
FOR loader = 1 TO lines
LINE INPUT #1, file(loader)
IF file(loader) = "" THEN file(loader) = " "
NEXT loader
CLOSE 1
FOR enforce = 1 TO lines
FOR check1 = 2 TO lines
tline$ = file(check1)
IF ASC(LCASE$(LEFT$(tline$, 1))) < ASC(LCASE$(LEFT$(file(check1 - 1), 1))) THEN SWAP file(check1), file(check1 - 1)
IF ASC(LCASE$(LEFT$(tline$, 1))) = ASC(LCASE$(LEFT$(file(check1 - 1), 1))) THEN
IF LEN(tline$) < LEN(file(check1 - 1)) THEN SWAP file(check1), file(check1 - 1)
IF LEN(tline$) = LEN(file(check1 - 1)) THEN
FOR check2 = 1 TO LEN(tline$)
IF ASC(LCASE$(MID$(tline$, check2, check2))) < ASC(LCASE$(MID$(file(check1 - 1), check2, check2))) THEN SWAP file(check1), file(check1 - 1)
NEXT check2
END IF
END IF
NEXT check1
NEXT enforce
OPEN file$ FOR APPEND AS 1
PRINT #1, blank$
PRINT #1, blank$
titlebar$ = "Alphabitazed by Jehovah"
PRINT #1, titlebar$
PRINT #1, blank$
FOR printer = 1 TO lines
PRINT #1, file(printer)
NEXT printer
CLOSE 1