sort list by date

Hi to all,

I have a list:

{"2008-4-10 and some text","2008-11-22 and some text","2008-4-5 and some text"}

How can I sort it in chronological order?

Thanks,

gecko

Hi gecko,

as «class isot» doesn’t work any more in Leopard,
this should work both in Tiger and Leopard and independent from international date format settings


set L to {"2008-4-10 and some text", "2008-11-22 and some text", "2008-4-5 and some text"}

tell (current date) to set cd to it - (its time)
set dateList to {}
set TID to text item delimiters
repeat with i in L
	set text item delimiters to space
	set theDate to text item 1 of i
	set text item delimiters to "-"
	tell cd to set {year, its month, day} to text items of theDate
	copy cd to end of dateList
end repeat
set text item delimiters to TID
sort_items(dateList, L)
L --> {"2008-4-5 and some text", "2008-4-10 and some text", "2008-11-22 and some text"}

to sort_items(|sortlist|, SecondList)
	script L
		property srt : |sortlist|
		property sec : SecondList
	end script
	tell (count L's srt) to repeat with i from (it - 1) to 1 by -1
		set s to (L's srt)'s item i
		set r to (L's sec)'s item i
		
		repeat with i from (i + 1) to it
			tell (L's srt)'s item i to if s > it then
				set (L's srt)'s item (i - 1) to it
				set (L's sec)'s item (i - 1) to (L's sec)'s item i
			else
				set (L's srt)'s item (i - 1) to s
				set (L's sec)'s item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > (L's srt)'s end then
			set (L's srt)'s item it to s
			set (L's sec)'s item it to r
		end if
	end repeat
end sort_items

What?

Yes, Sir
a bit more detailed:
a date coercion to an isot date works

(current date) as «class isot> as string

a literal string to an isot date doesn’t work, because of Leopard’s unique (Unicode) text class

set a to "2008-04-12"
a as «class isot»
--> Can't make "2008-04-12" into type «class isot».

Ah, of course.