Sorting list of records by one property

I’m at a loss, and nearly bald…
Palm Desktop doesn’t return appointments in chronological (or creation, for that matter) order, but I need to return them as such. Part of a much longer script returns a list of appointments in the following format:

{{apptTime:date "Friday, January 1, 1904 6:30:00 PM", apptTitle:"Huntington"}, {apptTime:date "Friday, January 1, 1904 8:30:00 PM", apptTitle:"Drive"}, {apptTime:date "Friday, January 1, 1904 4:00:00 PM", apptTitle:"Drive"}, {apptTime:date "Friday, January 1, 1904 8:30:00 AM", apptTitle:"MB Staff Meeting"}}

I need to sort them by apptTime, so I’d get a new list back:

{{apptTime:date "Friday, January 1, 1904 8:30:00 AM", apptTitle:"MB Staff Meeting"}, {apptTime:date "Friday, January 1, 1904 4:00:00 PM", apptTitle:"Drive"}, {apptTime:date "Friday, January 1, 1904 6:30:00 PM", apptTitle:"Huntington"}, {apptTime:date "Friday, January 1, 1904 8:30:00 PM", apptTitle:"Drive"}}

This is going to be deployed on an array of machines running OS8 through OSX 10.2, so I’d like to stick with as plain vanilla applescript as i can. I tried to write a binary insertion sort handler, but have only succeeded in making myself that much closer to bald…
Is there a simple way to sort a list of records by one property, that property being a time (date)?

: As a possible alternative to tet’s offering, here’s a fast
: insertion sort by Arthur Knapp, adapted here for your
: purposes:
Thanks, Nigel! That’s exactly what I wanted. I had initially started trying to code an insertion sort from scratch, and was tearing my hair out. I knew I was making it more difficult than it had to be.
thank you.
Also thank’s to tet. I tried to make his work (his code wouldn’t compile, but most of the ommitted end if’s and end repeats seemed obvious. However, at least at 2:00 am, I couldn’t figure out why it didn’t actually work…), and it looks like a simple approach. Simple is always good. Must have just been a copy/paste error…
Thanks again, Chad

I’m curious if you actually got this code to sort your Palm Desktop Datebook appointments chronologically by date. I tried both TET’s code and Nigel’s and neither one work (although they do compile, with some fiddling).

imadrummer

There seems to be some duff HTML above. Neither of the scripts transfer properly to Script Editor ” either by pasting or by clicking on “Open this Scriplet in your Editor:” ” and there’s quite a lot of text missing from tet’s script. I think it may be something to do with the different forum software this site used back in 2002. My script (Arthur’s, that is) does work. Here’s a repost, which hopefully will transfer better.

on sortRecordsbyApptTime(recordList) -- a list of records
	
	-- Make a copy of the list with "sentinal" (place-holder) record at the beginning of it
	set recordList to {item 1 of recordList} & recordList
	
	-- Insertion sort:
	repeat with i from 3 to recordList's length
		-- If a record from the original list has an earlier apptTime than its predecessor
		if recordList's item i's apptTime comes before recordList's item (i - 1)'s apptTime then
			-- Store it and replace it in the list with a non-record
			set r to recordList's item i
			set recordList's item i to missing value
			-- Go back through the list to find a record whose apptTime is the same or earlier,
			-- ending by default at the sentinal if a suitable candidate isn't found
			repeat with j from i - 2 to 1 by -1
				if r's apptTime does not come before recordList's item j's apptTime then exit repeat
			end repeat
			
			-- Make a new list, with the stored record inserted after the one just found and without the non-record
			set recordList to (recordList's items 1 thru j) & {r} & (recordList's records (j + 1) thru -1)
		end if
	end repeat
	
	-- Return a fully sorted list, minus the sentinal
	return the rest of recordList
	
end sortRecordsbyApptTime

sortRecordsbyApptTime({{apptTime:date "Friday 1 January 1904 18:30:00", apptTitle:"Huntington"}, {apptTime:date "Friday 1 January 1904 20:30:00", apptTitle:"Drive"}, {apptTime:date "Friday 1 January 1904 16:00:00", apptTitle:"Drive"}, {apptTime:date "Friday 1 January 1904 08:30:00", apptTitle:"MB Staff Meeting"}})
--> {{apptTime:date "Friday, 1 January 1904 08:30:00", apptTitle:"MB Staff Meeting"}, {apptTime:date "Friday, 1 January 1904 16:00:00", apptTitle:"Drive"}, {apptTime:date "Friday, 1 January 1904 18:30:00", apptTitle:"Huntington"}, {apptTime:date "Friday, 1 January 1904 20:30:00", apptTitle:"Drive"}}

Nigel, sorry - i’m not sure how that all came about or happened. We’ve been doing a lot of work on the board behind the scenes, so it potentially could of happened then.

Anyway, sorry for the foul up. :rolleyes:

No real problem, Ray. I was impressed that the “scriplet” facility was even partially available with old posts. :slight_smile:

heh :rolleyes:

We’re currently working on that too. :mad:

Just taking a lot of time, so please pardon the messy scripts. As we find stuff we try to fix it.