
'Compile as a WINDOWS target

$include "windowssdk.inc"

window d1
istring text[5000]
string a[2000]
string t1,t2
Uint hMem = 0
int x,y,z,startpos,endpos,count,length,h,g,a1,a2,b1,b2,position

SETID "CFTEXT",1

'Get clipboard data.
OpenClipboard(d1.hWnd)
hMem = GetClipboardData(@CFTEXT)
text = #<string>hMem
CloseClipboard()

'Split into lines for code for sorting.
do
    x ++
    string chr = ""
    chr = mid$(text,x,1)
    if asc(chr) = 13
       y ++ 
       endpos = x
       a[y] = mid$(text,startpos,endpos-startpos)
       startpos = endpos + 2
       x = endpos + 2
    endif
until x >= len(text)
y ++
a[y] = mid$(text,startpos,endpos-startpos) + chr$(10)      

'Sort the lines of code.
Sort()

'Build up the buffer again with the sorted lines of code.
for x = 1 to count

    if a[x] <> chr$(13) 

		 for y = 1 to len(a[x])
			  text = text + mid$(a[x],y,1)
		 next y 

       if mid$(a[x],y-1,1) <> chr$(13) 
			 if mid$(a[x],y-1,1) <> chr$(10)
		       text = text + chr$(13) + chr$(10)
          endif
       endif

    endif 

next x

'Open a window.
OPENWINDOW d1,0,0,995,730,@SIZE|@MINBOX|@MAXBOX,0,"",&Handler
CONTROL d1,@RICHEDIT,"",7,35,970,500,0x50B010C4,1

'Add the sorted text into the window control.
setcontroltext d1,1,text

'Set the text as selected by a user.
CONTROLCMD d1, 1, @RTSETSELECTION,0,-1

'Copy the sorted code back to the clipboard.
CONTROLCMD d1,1,@RTCOPY

'Wait a little then close.
sleep(2000)
closewindow d1
END

SUB Handler
SELECT @MESSAGE

		 CASE @IDCLOSEWINDOW
			   closewindow d1
			   end

		 CASE @IDCREATE
				CENTERWINDOW d1

ENDSELECT
RETURN 0
ENDSUB


sub Sort(),int
'Empty the text buffer.
text = ""
int NumberToSort = y

count = y

for x = 1 to 40
    a[count+1] = a[count+1] + chr$(126) 'Highest string value to sort against.
next x

'1st letter of the word sort e.g. (a)pple,(c)at,(d)og,(e)arth....(w)ater
'Sorts the words beginnig with A to Z

FOR x = 1 to count
FOR z = 1 to count

    length = 1
    position = 1
	 getval1()  'get the ASCII value of first letter
    checkltr() 'sort them from words beginnig with A to Z

NEXT z
NEXT x

'Check subsequent letters of all array entries
'both single letters and strings of letters
LABEL totloop

FOR y = 1 TO count
FOR z = 1 to count

    length = 1
    position = 1 
    getval1() 

    FOR h = 2 TO LEN(a[z])
		  IF LEN(a[z]) >= h
			  IF b1 = b2 'letter match
				  length = h
				  position = 1 
				  getval1()
				  checkltr()
			  ENDIF
		  ENDIF 
    NEXT h

NEXT z
NEXT y

return 0
endsub

SUB getval1 'Total up the ASCII values of each array for "g" positions 

a1 = 0
a2 = 0
b1 = 0
b2 = 0

FOR g = 1 TO length

a1 = ASC(LCASE$(MID$(a[z],g,1)))
b1 = b1 + a1 

a2 = ASC(LCASE$(MID$(a[z+1],g,1)))
b2 = b2 + a2

NEXT g

RETURN
ENDSUB

SUB checkltr() 

'Swap array entries over if needed 
'b1 & b2 hold the ASCII values of the current character or string of characters.

	IF b1 > b2 'To sort in decending order change to "b1 < b2"
	   t1 = a[z]
	   t2 = a[z+1]
	   a[z] = t2
	   a[z+1] = t1
	ENDIF 

RETURN 0
ENDSUB
