Sorting a list, this is what i have, but doesn't work

OK, i went the the applesceript resource sit and came across a sort list method which i needed. I changed the code to do what i want but it doesn’t work. What should happen is the script should read the file, sort it, then save the new sorted file to another file.
Here is what i have


--this script calls in a file and then sorts it
--returns the file with all the information sorted

--set the two filenames
property oldFile : ((path to desktop) as string) & "libraryno.txt"
property newFile : ((path to desktop) as string) & "newLibrary.txt"

--get the list
set fileList to read file oldFile as list using delimiter return
--call the sort method
sortTheFile(the fileList)

------define the sort method
on sortTheFile(fileList)
	try
		--sets 2 temp lists
		set the indexList to {}
		set the sortedList to {}
		repeat (the number of items in fileList) times
			set the lowItem to ""
			repeat with i from 1 to (number of items in fileList)
				if i is not in the indexList then
					set thisItem to item i of fileList as text
					if the lowItem is "" then
						set the lowItem to thisItem
						set the lowItemIndex to i
					else if thisItem comes before the lowItem then
						set the lowItem to thisItem
						set the lowItemIndex to i
					end if
				end if
			end repeat
			set the end of the sortedList to the lowItem
			set the end of the indexList to lowItemIndex
		end repeat
		return sortedList
		
		--call the save method
		saveTheSortedList(sortedList)
	on error this_error
		display dialog "The follwoing error occured:  " & return & this_error
	end try
	
end sortTheFile

-- save the sorted list to a new file.
on saveTheSortedList(sortedList)
	--this method opens the file for write and saves the list to it
	try
		open for access file newFile with write permission
		repeat (the number of items in sortedList) times
			set thisItem to i of sortedList as string
			write (thisItem & return) to file newFile starting at eof
			close access file newFile
		end repeat
	on error the_error
		try
			close access newFile
		end try
		display dialog "The follwing error occured:  " & return & the_error
	end try
	
	
end saveTheSortedList


the text that i wish to sort is this:


000372-OVW.jpg
000376-OVW.jpg
000387-OVW.jpg
Tabarly4-Keith.Taylor.jpg
Velsheda.Marguerite-T.jpg
Velsheda.Blown.Sail.jpg
Velsheda.Antigua.98.jpg
Velsheda.Antigua.98-2.jpg
000238-OVW.jpg
000266-OVW.jpg
000268-OVW.jpg

It is just a snippet but that is what the file will be like anyway.

Any ideas??? i dont know if it does work, it did before, but it doesnt save the new file

No.

Jon

What happens if you use this (untested)?

on saveTheSortedList(sortedList)
	--this method opens the file for write and saves the list to it 
	try
		open for access file newFile with write permission
		repeat with i from 1 to (the number of items in sortedList)
			set thisItem to (item i of sortedList) as string
			write (thisItem & return) to file newFile starting at eof
			--close access file newFile
		end repeat
		close access file newFile
	on error the_error
		try
			close access file newFile
		end try
		display dialog "The following error occured:  " & return & the_error
	end try
end saveTheSortedList

– Rob

hasve tried the two suggestions, but still no luck, with johns it writes a list to the file, although not really sorted, and with robs nothing really happens.

It looks to me as if the code stumbles and doesn’t know what to do with the number files, although i th9ought that it would be able to do something with them, like place them at the start like all sort methods.

No luck.

I will try again, but if anyone might know why, i am using johnns code, then please let me know thanks. The ouput from the file is: (oly a few lines)


Tabarly3-DG.jpg
Tabarly2-DG.jpg
A.Colas/Teura.1976.DG.jpg
mme.tabarly-dg.jpg
Kids.in.Cowes-AF.jpg
Isle.of.Wight.Map-AF.jpg
velsheda-af.jpg
A.Colas.1976-DG.jpg
009346-DA.jpg
009315-DA.jpg
009314-DA.jpg

It is like that all the way down, sorting weird.

This type of script is where the advantage of OSX comes into play: use the “do shell script” command and the unix command line utility “sort”. I have to wait until I get home for the exact code, but it works very well (and is much faster than any sort you could write in AppleScript, assuming you know nothing about the order of the lines in the file).

yeah that would be nice thanks.

Can i do a binary search with shell?? actually dont worry i can just use the search function in the actuall txt editor.

Well, this wait wait kind of a let down. The entire script to sort a file is:

do shell script "sort -o " & theFile & " -u " & theFile

The first filename is the output file, the second the input file. The -u option makes sure no two items are the same.

If theFile is not in a shell passable form, you will need to use “quoted form of”. If it is a non-POSIX path, you will need to use “POSIX path of”. Play around with these and always work on a copy.