
DEF f3:window
DEF dj$,mj$,yj$:string 
DEF Dates[5]:STRING
Dates = "29-07-2013","05-08-2013","10-08-2013","01-07-2013","30-06-2013"

DEF Days[5]:STRING
Days = "02","05","31","15","04"

DEF Desc[5]:STRING
Desc = "29th of July","5th of August","10th of August","1st of July","30th of June"


OPENWINDOW f3,250,120,700,400,@MINBOX|@MAXBOX|@SIZE,0," Sort dates example",&handler
CENTERWINDOW f3
floodfill f3,1,1,rgb(43,85,142)

'Holiday listview box

'Note - This one will appear on screen and is not sorted!
CONTROL f3,@LISTVIEW,"",100,35,400,150,@VSCROLL|@CTLISTNOTIFY,747
CONTROLCMD f3,747,@LVINSERTCOLUMN,0,"Holiday dates"
CONTROLCMD f3,747,@LVINSERTCOLUMN,1,"days"
CONTROLCMD f3,747,@LVINSERTCOLUMN,2,"description"
CONTROLCMD f3, 747, @LVSETCOLWIDTH, 0, 120
CONTROLCMD f3, 747, @LVSETCOLWIDTH, 1, 50
CONTROLCMD f3, 747, @LVSETCOLWIDTH, 2, 220

'Note - This one will NOT appear on screen AND is sorted!
CONTROL f3,@LISTVIEW,"",600,35,400,150,@VSCROLL|@LVSSORTASCENDING|@CTLISTNOTIFY,7767
CONTROLCMD f3,7767,@LVINSERTCOLUMN,0,""
CONTROLCMD f3, 7767, @LVSETCOLWIDTH, 0, 400

'Hide the second listbox
SHOWWINDOW f3,@SWHIDE,7767

CONTROL f3,@BUTTON,"Sort",100,300,80,30,0x50000000,200
CONTROL f3,@BUTTON,"Original",200,300,80,30,0x50000000,201
CONTROL f3,@BUTTON,"Quit",300,300,80,30,0x50000000,202

CONTROL f3,@STATIC,"Click ' Sort ' to sort the dates and corresponding details.",100,200,400,25,@CTEDITLEFT,1
SETCONTROLCOLOR f3,1,rgb(255,255,255), rgb(43,85,142)

CONTROL f3,@STATIC,"Click ' Original ' for inital state.",100,200,400,25,@CTEDITLEFT,2
SETCONTROLCOLOR f3,2,rgb(255,255,255), rgb(43,85,142)

SHOWWINDOW f3,@SWHIDE,2

SETFONT f3,"Arial",12,500,0,1
SETFONT f3,"Arial",12,500,0,2
SETFONT f3,"Arial",12,500,0,200
SETFONT f3,"Arial",12,500,0,201
SETFONT f3,"Arial",12,500,0,202
SETFONT f3,"Arial",12,500,0,747

GOSUB unsorted

WAITUNTIL f3=0
END

SUB handler(),int 
	SELECT @MESSAGE
		CASE @IDINITDIALOG
			CENTERWINDOW f3
			/* Initialize any controls here */
		CASE @IDCLOSEWINDOW
			CLOSEWINDOW f3
			END

		CASE @IDCONTROL
			SELECT @CONTROLID

				CASE 200 'Sort
					IF @NOTIFYCODE = 0
						GOSUB Holidays
						SHOWWINDOW f3,@SWHIDE,1
						SHOWWINDOW f3,@SWSHOW,2
						SETFOCUS f3
                        RETURN 0
					ENDIF

				CASE 201 'unsort
					IF @NOTIFYCODE = 0
						GOSUB unsorted
						SHOWWINDOW f3,@SWHIDE,2
						SHOWWINDOW f3,@SWSHOW,1
						SETFOCUS f3
                        RETURN 0
					ENDIF

				CASE 202 'Quit
						CLOSEWINDOW f3
						END

			ENDSELECT

	ENDSELECT
RETURN
ENDSUB


'Sub routine for sorting the holiday dates
SUB Holidays(),int 

DEF item,vlcount:int
DEF jn$:string  
DEF holdaylist2,storedate$,daysout,descout:string 
storedate$ = ""
jn$ = ""

'Delete all entries for the listboxes
CONTROLCMD f3,7767,@LVDELETEALL
CONTROLCMD f3,747,@LVDELETEALL


item = 0
vholcount = 0
vlcount = -1

'Read the holiday file 
FOR item = 0 TO 4
JN = Julian(VAL( MID$(Dates[item],7,4)),VAL( MID$(Dates[item],4,2)),VAL( MID$(Dates[item],1,2)))
'Convert the Julian number to a string for saving
jn$ = STR$(jn)

'Save the string to the "hidden" listbox as jn$ AND also save the number of days holiday AS a complete single string
'These entries will be automatically sorted.
CONTROLCMD f3,7767,@LVINSERTITEM,item,jn$+days[item]+desc[item]
'Here i'm simply counting the number of holidays booked / used by an employee - not important in this example

'Keep on reading until no more holidays to check
NEXT item


'Now get the number of holiday dates e.g. 5 different holiday dates
hiddencount = CONTROLCMD( f3, 7767, @LVGETCOUNT)

item = 0

'Read through the "hidden" sorted listbox
FOR item = 0 TO hiddencount-1

'Get the text - "24564452" where 2456445 is the Julian date AND 2 is the number of days holiday.
CONTROLCMD( f3, 7767, @LVGETTEXT, item,0,holdaylist2)

'I found you have to trim leading spaces first
holdaylist2 = LTRIM$(holdaylist2)

'Now get the julian date i.e. the first 7 characters of the string
jn = VAL(MID$(holdaylist2,1,7))

'Convert the number back to a date e.g. 26th October 2013 dj$ = 26 mj$ = 10 yj$ = 2013
JultoDate(jn)

'Add to the listview on screen - 2 columns "Holiday date" and "Days"
CONTROLCMD f3,747,@LVINSERTITEM,item,dj$ + "-" + mj$ + "-" + yj$ 

daysout = MID$(holdaylist2,8,2)
CONTROLCMD f3,747,@LVSETTEXT,item,1,daysout

descout = MID$(holdaylist2,10,100)
CONTROLCMD f3,747,@LVSETTEXT,item,2,descout

'Keep on adding to the listbox until all dates are in
NEXT item

RETURN 0
ENDSUB

'--------------------------
'Sub routines you will need
'--------------------------

'Converts a date to a Julian number
Sub Julian(year :int, month :int, day : int),int
def a, jd, m, y : int
a = (14 - month)/12
y = year + 4800 - a
m = month + 12 * a - 3
jd = day + (153 * m + 2)/5 + y * 365 + y/4 - y/100 + y/400 - 32045
RETURN jd
endsub 

'Converts a Julian number to a date
Sub JulToDate(JN As Int),String

	JulNumber = JN + 68569
	help = 4 * JulNumber / 146097
	JulNumber = JulNumber - (146097 * help + 3) / 4
	Tempyj = 4000 * (JulNumber + 1) / 1461001
	JulNumber = JulNumber - (1461 * Tempyj / 4) + 31
	Tempmj = 80 * JulNumber / 2447
	dj = (JulNumber - (2447 * Tempmj / 80))
	mj = (Tempmj + 2 - (12 * (Tempmj / 11)))
	yj = (100 * (help - 49) + Tempyj + (Tempmj / 11))
	mj$ = Using("0##",mj) 
	dj$ = Using("0##",dj)
	yj$ = Using("0####",Int(Abs(yj)))
	If yj < 0 Then yj$ = "-" + yj$
	Return mj$+dj$+yj$
EndSub

SUB unsorted

CONTROLCMD f3,7767,@LVDELETEALL
CONTROLCMD f3,747,@LVDELETEALL

FOR n = 0 to 4
CONTROLCMD f3,747,@LVINSERTITEM,n,Dates[n]
CONTROLCMD f3,747,@LVSETTEXT,n,1,Days[n]
CONTROLCMD f3,747,@LVSETTEXT,n,2,Desc[n]
NEXT n

RETURN
ENDSUB