Some time ago (can’t find the thread) Kai posted a handler for sorting a list of file names according to their modification dates. I’m trying to use that to sort a list of calendar events according to the date of the event. I’ve boiled it down to this:
set tList to {4, 1, 5, 3, 6, 2} -- in the order of the dates below.
set tDates to {date "Tuesday, June 6, 2006 11:00:00 AM", date "Thursday, April 20, 2006 12:00:00 PM", date "Wednesday, July 12, 2006 12:00:00 PM", date "Tuesday, May 30, 2006 12:00:00 PM", date "Tuesday, August 1, 2006 11:30:00 AM", date "Wednesday, May 17, 2006 12:00:00 PM"}
sort_items(tList, tDates)
to sort_items(theList, sort_by) -- Kai's dual list sorter
script o
property tL : theList
property sB : sort_by
end script
repeat with j from 2 to count o's tL
set v to o's sB's item j
set w to o's tL's item j
repeat with j from j to 2 by -1
set j to j - 1
tell o's sB's item j to if v < it then
set o's sB's item j to it
set o's tL's item j to o's tL's item j
else
set o's sB's item j to v
set o's tL's item j to w
exit repeat
end if
end repeat
if j is 2 and v < o's sB's item 1 then
set o's sB's item 1 to v
set o's tL's item 1 to w
end if
end repeat
end sort_items
This returns two lists now sorted and both are wrong (though they match, of course). tList, which should have come out as {1, 2, 3, 4, 5, 6} is actually found to be: {4, 5, 5, 6, 6, 2}.
Where have I gone wrong?