Sorting ical events by starting date

Hi all!

I am using “CalendarLib EC” to extract future events (for the next 7 days) from different calendars (of iCal).

At the end I get a list of events which are not sorted as I want.
Is there any way to sort events by " starting date"?

The script I am developing is:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "CalendarLib EC" version "1.1.3" -- put this at the top of your scripts

set choosedCals to {"Home", "Personal", "Not_Sync", "Urgent_Lux", "Work"}

set d1 to current date
set d2 to d1 + 7 * days
set theStore to fetch store
set AllEvents to {}
set AllEventsFlat to {}
set AllCals to {}
set MaxEvent to 8
repeat with y in choosedCals
	set end of AllCals to fetch calendar y cal type cal cloud event store theStore -- change to suit
end repeat
repeat with z in AllCals
	set myEvents to fetch events starting date d1 ending date d2 searching cals {z} event store theStore
	if (count of myEvents's items) > 0 then
		set end of AllEvents to fetch events starting date d1 ending date d2 searching cals {z} event store theStore
		set AllEventsFlat to (AllEventsFlat & (fetch events starting date d1 ending date d2 searching cals {z} event store theStore))
	end if
end repeat
# let's remove all day-long events
set AllEventsFlat to filter events event list AllEventsFlat without runs all day
# let's sort all events based on starting date
set MySortingList to {}
repeat with anEvent in AllEventsFlat
	anEvent
	set InfoEvent to (event info for event anEvent)
	set end of MySortingList to {start_date:(event_start_date of InfoEvent), eventID:(event_external_ID of InfoEvent)}
end repeat

As you will see at the end of the script, I was creating a list for each event by extracting “event_start_date” and “event_external_ID”.
My thinking was: if I can sort this list based on the date, then I will also sort the event_external_ID.
I am not sure I have explained myself.
Thanks.
L.

Add a use framework “Foundation” statement and this:

set anArray to current application's NSArray's arrayWithArray:MySortingList
set theDesc to current application's NSSortDescriptor's sortDescriptorWithKey:"start_date" ascending:true
set sortedList to (anArray's sortedArrayUsingDescriptors:{theDesc}) as list

If all you want is the sorted IDs, then change the last line to this:

set sortedList to ((anArray's sortedArrayUsingDescriptors:{theDesc})'s valueForKey:"eventID") as list

Beautiful !!!
Thanks !

PS: I must admit I feel like God on one side and mortals on the other.
Gods are those that know “Foundation”
Yesterday, I wasted two hours trying to adjust this sorting algorithm (see below) to list of records, obviously without success. And the only thing I got was an headache … :wink:

But anyway, thanks a lot Shane !

set myText to {{start_date:date "Friday, 11 October 2019 at 18:30:00", eventID:"B8423830-B963-46BF-95EE-DB74A06A61C0"}, {start_date:date "Monday, 14 October 2019 at 18:30:00", eventID:"5289A64A-1338-44B5-A4B7-22A2B93FA3E7"}, {start_date:date "Monday, 14 October 2019 at 16:00:00", eventID:"D78ABAE7-FAC2-4CBE-9610-1B6C1FBAC686"}, {start_date:date "Wednesday, 16 October 2019 at 15:00:00", eventID:"30847B03-97CA-48EA-A652-773370F638DE"}}

sort_items2(myText)


on sort_items2(l)
	tell (count l) to repeat with i from (it - 1) to 1 by -1
		set s to l's item i
		repeat with i from (i + 1) to it
			tell l's item i to if s > it then
				set l's item (i - 1) to it
			else
				set l's item (i - 1) to s
				exit repeat
			end if
		end repeat
		if it is i and s > l's end then set l's item it to s
	end repeat
end sort_items2