I needed to sort a list in date order of ical events so i came up with these sort routines.
mind you it is slow(and gets even slower at an exponential rate) but i’ve put it through some paces and it seems to still work even on big lists it just takes a little while, it is lightning fast on say 20 events though.
on findLeastItem(lst)
tell application "iCal"
set theLeast to start date of item 1 of lst
set theIndex to 1
set iterater to 1
repeat with i in lst
if start date of i ≤ theLeast then
set theLeast to start date of i
set theIndex to iterater
end if
set iterater to iterater + 1
end repeat
return theIndex
end tell
end findLeastItem
on removeItemAtIndex(lst, theIndex)
set newList to {}
set theLength to length of lst
if theLength = 1 then
set newList to {}
else if theLength = theIndex then
set newList to items 1 thru (theLength - 1) of lst
else if theIndex = 1 then
set newList to items 2 thru theLength of lst
else
set newList to items 1 thru (theIndex - 1) of lst & items (theIndex + 1) thru (theLength) of lst
end if
return newList
end removeItemAtIndex
on sortEvents(myList)
set myNewList to {}
repeat until length of myList = 0
set leastIndex to findLeastItem(myList)
set end of myNewList to item leastIndex of myList
set myList to removeItemAtIndex(myList, leastIndex)
end repeat
return myNewList
end sortEvents
on getSecondsOfDays(Ndays)
return ((current date) - (3600 * 24 * Ndays))
end getSecondsOfDays
--And to call this i use
tell app "iCal"
set weeksHours to every event of calendar "Hours" whose start date is greater than my getSecondsOfDays(6)
set weeksHours to my sortEvents(weeksHours)
end tell
Model: MacBook Pro
Browser: Safari 419.3
Operating System: Mac OS X (10.4)