Sorting files by date and time

tell application "Finder"
	activate
	set pth to choose folder
	set filelist to files in pth
	set filelist1 to sort filelist by creation date
end tell

I use the code above to sort a list of files.
It sorts by the date correctly but not by the time.

Is there a way to sort the list by date and time in the correct order AM to PM?

Here’s my go at it:

tell application "Finder"
	activate
	set pth to choose folder
	set filelist to files in pth as alias list
	set CD to {}
	repeat with f in filelist
		set C to creation date of f
		set end of CD to my makeStamp(C, "", "", "")
	end repeat
end tell

set {timeSort, fileSort} to sort2Lists(CD, filelist)
fileSort

to sort2Lists(sortList, SecondList)
	tell (count sortList) to repeat with i from (it - 1) to 1 by -1
		set s to sortList's item i
		set r to SecondList's item i
		repeat with i from (i + 1) to it
			tell sortList's item i to if s > it then
				set sortList's item (i - 1) to it
				set SecondList's item (i - 1) to SecondList's item i
			else
				set sortList's item (i - 1) to s
				set SecondList's item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > sortList's end then
			set sortList's item it to s
			set SecondList's item it to r
		end if
	end repeat
	return {sortList, SecondList}
end sort2Lists

to makeStamp(Now, DelimD, DelimT, DelimDT)
	tell Now to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
		to set dateStamp to text -2 thru -1 & DelimD & text 4 thru 5 & DelimD & text 2 thru 3
	tell Now to tell ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as string) ¬
		to set timeStamp to text 2 thru 3 & DelimT & text 4 thru 5 & DelimT & text 6 thru 7
	return dateStamp & DelimDT & timeStamp
end makeStamp

David:

Here’s a quickie. It only returns names of files not actual file references but it’s still usable. And it does respect Time in the sort.


set sortedFiles to paragraphs of (do shell script "ls -t " & (quoted form of (POSIX path of (choose folder))))

If you want it in reverse. (that’s a lowercase “r” - if you use uppercase it will recursively read through directories (which you may or may not want).


set sortedFiles to paragraphs of (do shell script "ls -tr " & (quoted form of (POSIX path of (choose folder))))

and if you want to verify the Time is being read correctly.


set sortedFiles to paragraphs of (do shell script "ls -lt " & (quoted form of (POSIX path of (choose folder))))

. do this on a folder of files that were worked on today or you won’t see a useful time stamp.

Hope this is useful.

Have Fun!
Jim Neumann
BLUEFROG

I don’t understand the question. The times contribute to the date values, so if the files are sorted correctly by date, they must be sorted correctly by time too. I notice you’re sorting by creation date. Do you perhaps mean to sort by modification date?

@Nigel – the original script in this thread does not sort the files in a chosen folder by time on my machine; date, yes, time of day within a day, no; hence my kluge.

My apologies, then. It must be a bug in Leopard. But Adam, is it a Finder bug or an AppleScript date bug? For instance, if you simply set the end of CD to C in your workround, do you still get the right results?