one-loop sorting of strings by their length

Post your working scripts, libraries and tools
rommmcek
Posts: 391
Joined: 15 Aug 2014, 15:18

Re: one-loop sorting of strings by their length

21 Nov 2017, 20:12

New version of test. Better display of results. Added new binary function by Hegef. Hope no new bugs! Wordlist & description in posts above.

Please use code below!

P.s.: Attachment below only for Helgef as a remuneration for his commitments on this thread, because he has used emoji with Santa Clause cap and we are at the end of the year. (Experimental as a byproduct of an other code which included sorting)
Attachments
All_I_Want_For_Christmas_Is_AutoHotkey.ahk
(535.4 KiB) Downloaded 28 times
Last edited by rommmcek on 02 Dec 2017, 07:40, edited 1 time in total.
Helgef
Posts: 3303
Joined: 17 Jul 2016, 01:02
Contact:

Re: one-loop sorting of strings by their length

23 Nov 2017, 17:56

Thank you very much for the beautiful song, very impressive! :xmas:
Minor comments, g and f should use byref, and you need to omit "`r" for the strsplit after sort_len. I made a bug fix to the sort_len function :arrow: github. The bug didn't matter in your tests though. :oops:

Cheers
User avatar
jeeswg
Posts: 5435
Joined: 19 Dec 2016, 01:58
Location: UK

Re: one-loop sorting of strings by their length

23 Nov 2017, 18:18

Yes, the song is very good, it is pretty spectacular. I had it on repeat, it helped me do some coding the other day.
How about these 2 (very short) songs? You could start a new thread.
- Bottles of beer on the wall... [4 lines or less challenge] - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 61#p181761
- 10 Green Bottles.
Cheers. :xmas:
rommmcek
Posts: 391
Joined: 15 Aug 2014, 15:18

Re: one-loop sorting of strings by their length

24 Nov 2017, 06:38

@Hegef: As the present should be!
P.s.: I postponed sorting tests because of music. (The most pleasant first!)
@Jeeswg: Should be good for any music (even if it still needs some (a lot of) work)!
Note, that I'm no musician, so I depend on others work.
P.s.: I've been thinking about new thread, but the code is real mess at the moment.
rommmcek
Posts: 391
Joined: 15 Aug 2014, 15:18

Re: one-loop sorting of strings by their length

02 Dec 2017, 07:38

New update for tests.
Functions f() & g() now have ByRef to ensure using same memory location for function's parameter & variable.
I abandoned ending up with arrays! (I think it has actually not much to do with sorting of strings by their length), but thank you Helgef, for reminding me "`r" should be omitted in StrSplit()
For display I'm using now Notepad choosing Consolas Font. It will display entire list. (No need for editing the wordlist, hope it'll work for everybody!)

Code: Select all

#NoEnv
SetWorkingDir, %A_ScriptDir%
CoordMode, ToolTip, Screen
SetBatchLines, -1                 ;       ################################################
FileRead, text_arr, wordslist.txt ; <==== attention typo for "wordlist", (not changed now)
the_arr:=RTrim(text_arr, "`r`n")
loop, 89
	A_Index<89 ? Spc.=A_Space : Spc.="`n"
ToolTip, % Spc "Test is running...`n " , 0, 0
; ========================
st:=A_TickCount
the_array:=StrSplit(the_arr, "`n", "`r")
the_sorted_arr_acending:=sort_by_length2(the_array)
Arrays:=A_TickCount-st  "	ms   for arrays"
ToolTip % Spc Arrays "`n`nTest is running...`n ", 0, 0
; ========================
st:=A_TickCount
the_array:=StrSplit(the_arr, "`n", "`r")
text_array1:=sort_by_length3(the_array)
Arr_app:=A_TickCount-st "	ms   for array & append"
ToolTip % Spc Arrays "`n" Arr_app "`n`nTest is running...`n ", 0, 0
;~ ; ========================
st:=A_TickCount
text_array2:=SmallestFirst(the_arr)
Loops:=A_TickCount-st "	ms   for loops"
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n`nTest is running...`n ", 0, 0
; ========================
st:=A_TickCount
the_array:=StrSplit(the_arr, "`n", "`r")
text_array3:=sort_by_length4(the_array)
Arr_ass1:=A_TickCount-st "	ms   for append - array assisted1"
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n" Arr_ass1 "`n`nTest is running...`n ", 0, 0
; ========================
st:=A_TickCount
the_array:=StrSplit(the_arr, "`n", "`r")
text_array4:=f(the_array)
Arr_ass2:=A_TickCount-st "	ms   for append - array assisted2"
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n" Arr_ass1 "`n" Arr_ass2 "`n`nTest is running...`n ", 0, 0
; ========================
st:=A_TickCount
text_array5:=g(the_arr)
Arr_ass3:=A_TickCount-st "	ms   for append - array assisted3"
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n" Arr_ass1 "`n" Arr_ass2 "`n" Arr_ass3 "`n`nTest is running...`n ", 0, 0
; ========================
text_array10:=text_arr
st:=A_TickCount
sort_len(text_array10, "`r`n", shortFirst:=true, omitEmpty:=true)
;text_array10:=StrSplit(input_arr, "`n", "`r")
Ahk_bin:=A_TickCount-st "	ms   for ahk & binary"
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n" Arr_ass1 "`n" Arr_ass2 "`n" Arr_ass3 "`n" Ahk_bin "`n`nTest is running...`n ", 0, 0
; ========================
text_array6:=the_arr
st:=A_TickCount
Sort, text_array6, f ShortestFirst1
text_array6:=RTrim(text_array6, "`r" "`n") ;text_array6:=StrSplit(input_arr, "`n", "`r")	
Srt1:=A_TickCount-st "	ms   for Sort1"
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n" Arr_ass1 "`n" Arr_ass2 "`n" Arr_ass3 "`n" Ahk_bin "`n" Srt1 "`n`nTest is running...`n ", 0, 0
; ========================
text_array7:=the_arr
st:=A_TickCount
Sort, text_array7, f ShortestFirst2
text_array7:=RTrim(text_array7, "`r" "`n") ;text_array7:=StrSplit(input_arr, "`n", "`r")	
Srt2:=A_TickCount-st "	ms   for Sort2"
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n" Arr_ass1 "`n" Arr_ass2 "`n" Arr_ass3 "`n" Ahk_bin "`n" Srt1 "`n" Srt2 "`n`nTest is running...`n ", 0, 0
; ========================
text_array8:=the_arr
st:=A_TickCount
Sort, text_array8, f ShortestFirst3
text_array8:=RTrim(text_array8, "`r" "`n") ;text_array8:=StrSplit(input_arr, "`n", "`r")	
Srt3:=A_TickCount-st "	ms   for Sort3"
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n" Arr_ass1 "`n" Arr_ass2 "`n" Arr_ass3 "`n" Ahk_bin "`n" Srt1 "`n" Srt2 "`n" Srt3 "`n`nTest is running...`n ", 0, 0
; ========================
text_array9:=the_arr
st:=A_TickCount
Sort, text_array9, f ShortestFirst4
text_array9:=RTrim(text_array9, "`r" "`n") ;text_array9:=StrSplit(input_arr, "`n", "`r")	
Srt4:=A_TickCount-st "	ms   for Sort4"
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n" Arr_ass1 "`n" Arr_ass2 "`n" Arr_ass3 "`n" Ahk_bin "`n" Srt1 "`n" Srt2 "`n" Srt3 "`n" Srt4 "`n`nPress Esc for next display!`n ", 0, 0
; ========================

Run, notepad
WinWaitActive, ahk_class Notepad,, 1
WinMove, ahk_class Notepad,, % A_ScreenWidth//2, 0, % A_ScreenWidth//2, A_ScreenHeight
WinMenuSelectItem, ahk_class Notepad,, 3&, 2& ;~ Send, !of
WinWaitActive, Font,, 1
Send, consolas{Enter}
WinWaitActive, ahk_class Notepad,, 1
; ========================
WinSetTitle, ahk_class Notepad,, unsorted data
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_arr, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle, unsorted data ahk_class Notepad,, arrays
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  the_sorted_arr_acending, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  arrays ahk_class Notepad,, array & append
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array1, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  array & append ahk_class Notepad,, loops
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array2, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  loops ahk_class Notepad,, append - array assisted1
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array3, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  append - array assisted1 ahk_class Notepad,, append - array assisted2
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array4, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  append - array assisted2 ahk_class Notepad,, append - array assisted3
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array5, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  append - array assisted3 ahk_class Notepad,, ahk & binary
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array10, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  ahk & binary ahk_class Notepad,, Sort1
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array6, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  Sort1 ahk_class Notepad,, Sort2
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array7, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  Sort2 ahk_class Notepad,, Sort3
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array8, ahk_class Notepad
KeyWait, Esc, d
; ========================
WinSetTitle,  Sort3 ahk_class Notepad,, Sort4
ControlSetText, Edit1, Loading data...`, Please wait!, ahk_class Notepad
ControlSetText, Edit1, %  text_array9, ahk_class Notepad
; ========================
ToolTip % Spc Arrays "`n" Arr_app "`n" Loops "`n" Arr_ass1 "`n" Arr_ass2 "`n" Arr_ass3 "`n" Ahk_bin "`n" Srt1 "`n" Srt2 "`n" Srt3 "`n" Srt4 "`n`nPress Esc to exit!`n ", 0, 0
SoundBeep
KeyWait, Esc, D
ToolTip
WinClose, ahk_class Notepad
exitapp

;~ if (the_array.MaxIndex()<=20000) {
;~ ScrollBox("Unsorted:`n`n" st_printarr(the_array),"P")
;~ ScrollBox("Sorted ascending1:`n`n " . st_printarr(the_sorted_arr_acending),"P")
;~ ScrollBox("Sorted ascending2:`n`n " . st_printarr(text_array1),"P")
;~ ScrollBox("Sorted SmallestFirst1:`n`n " . st_printarr(text_array2),"P")
;~ ScrollBox("Sorted SmallestFirst2:`n`n " . st_printarr(text_array3),"P")
;~ ScrollBox("Sorted SmallestFirst3:`n`n " . st_printarr(text_array4),"P")
;~ ScrollBox("Sorted SmallestFirst4:`n`n " . st_printarr(text_array5),"P")
;~ ScrollBox("Sorted ShortestFirst1:`n`n " . st_printarr(text_array6),"P")
;~ ScrollBox("Sorted ShortestFirst2:`n`n " . st_printarr(text_array7),"P")
;~ ScrollBox("Sorted ShortestFirst3:`n`n " . st_printarr(text_array8),"P")
;~ ScrollBox("Sorted ShortestFirst4:`n`n " . st_printarr(text_array9),"P")
;~ ScrollBox("Sorted ShortestFirst5:`n`n " . st_printarr(text_array10),"P")
;~ ScrollBox("Unsorted:`n`n" st_printarr(the_array),"P")
;~ } 


;String Things by tidbit
;https://autohotkey.com/boards/viewtopic.php?f=6&t=53&sid=5c401643235e7a2e73d769f1e5deac0f
st_printArr(array, depth=5, indentLevel="")
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}



; ScrollBox by Fanatic Guru v 2015 04 29  Version 1.10
;https://autohotkey.com/boards/viewtopic.php?f=6&t=4837
ScrollBox(String := "", Options := "", Label := "")
{
	Static Gui_List, Gui_Index
	DetectHiddenWindows, % (Setting_A_DetectHiddenWindows := A_DetectHiddenWindows) ? "On" :
	SetWinDelay, % (Setting_A_WinDelay := A_WinDelay) ? 0 : 0
	if !Gui_List
		Gui_List := {}
	if Label
	{
		Gui_Label := "ScrollBox_Gui_Label_" RegExReplace(Label, "i)[^0-9a-z#_@\$]", "")
		Gui_Hwnd := Gui_List[Gui_Label]
		Win_Hwnd := DllCall("GetParent", UInt, Gui_Hwnd)
		if RegExMatch(RegExReplace(Options, "\{.*}"), "i)d")
			Gui, %Gui_Label%:Destroy
		else if WinExist("ahk_id " Win_Hwnd)
		{
			if String
				GuiControl,,%Gui_Hwnd%, %String%
			WinGetPos, WinX, WinY, WinW, WinH
			if RegExMatch(Options, "i)x(\d+)", Match)
				WinX := Match1
			if RegExMatch(Options, "i)y(\d+)", Match)
				WinY := Match1
			if RegExMatch(Options, "i)w(\d+)", Match)
				WinW := Match1
			if RegExMatch(Options, "i)h(\d+)", Match)
				WinH := Match1
			Winmove, ahk_id %Win_Hwnd%,, WinX, WinY, WinW, WinH
			if RegExMatch(Options, "i)h(?!\d)", Match)
				Gui, %Gui_Label%:Hide
			if RegExMatch(Options, "i)s", Match)
				Gui, %Gui_Label%:Show
			DetectHiddenWindows, %Setting_A_DetectHiddenWindows%
			SetWinDelay, %Setting_A_WinDelay%
			return
		}
	}
	else
	{
		Gui_Index ++
		Gui_Label := "ScrollBox_Gui_Label_" Gui_Index
	}
	if (!String and !Options)
	{
		if Label
		{
			Gui_List.Delete(Gui_Label)
			Gui, %Gui_Label%:Destroy
		}
		else
		{
			for key, element in Gui_List
				Gui, %key%:Destroy
			Gui_List := {}
		}
		DetectHiddenWindows, %Setting_A_DetectHiddenWindows%
		SetWinDelay, %Setting_A_WinDelay%
		return
	}
	Gui %Gui_Label%:Default
	Gui +LabelAllGui
	Adjust1 := 10
	ButtonPushed := -2
	if RegExMatch(Options, "i)f(\d+)", Match)
	{
		Gui, Font, s%Match1%
		Adjust1 := Match1
	}
	else if RegExMatch(Options, "i)f\{(.*)}", Match)
	{
		Options := RegExReplace(Options, "i)f\{.*}")
		StringSplit, Match, Match1, `,
		Gui, Font, %Match1%, % Trim(Match2)
		RegExMatch(Match1, "i)s(\d+)", Adjust)
	}
	else
		Gui, Font
	Gui, Margin, 20, 20
	Gui, +MinSize200x200 +Resize
	Gui, Color, FFFFFF
	Opt := "hwndGui_Hwnd ReadOnly -E0x200  "
	if !(Options ~= "i)w(?!\d)")
		Opt .= "+0x300000 -wrap "
	if RegExMatch(Options, "i)h(\d+)", Match)
		Opt .= "h" Match1 " ", Control := true 
	if RegExMatch(Options, "i)w(\d+)", Match)
		Opt .= "w" Match1 " ", Control := true
	if (Options ~= "i)c")
		Opt .= "center "
	if (Options ~= "i)l")
		Opt .= "left "
	if (Options ~= "i)r")
		Opt .= "right "
	Opt_Show := "AutoSize "
	if RegExMatch(Options, "i)x(\d+)", Match)
		Opt_Show .= "x" Match1 " "
	if RegExMatch(Options, "i)y(\d+)", Match)
		Opt_Show .= "y" Match1 " "
	if Control
	{
		Gui, Add, Edit, % Opt
		GuiControl, , %Gui_Hwnd%, %String%
	}
	else
		Gui, Add, Edit, % Opt, %String%
	if RegExMatch(Options, "i)b(1|2)", Match)
	{
		Button := Match1
		if (Button = 1)
			Gui, Add, Button, gAllGuiButtonOK hwndScrollBox_Button1_Hwnd Default, OK
		else
		{
			Gui, Add, Button, gAllGuiButtonYES hwndScrollBox_Button1_Hwnd Default, YES
			Gui, Add, Button, gAllGuiButtonNO hwndScrollBox_Button2_Hwnd, % " NO "
		}
	}
	Gui, Show, % Opt_Show, % Label ? Label : "ScrollBox"
	Gui_List[Gui_Label] := Gui_Hwnd
	Win_Hwnd := DllCall("GetParent", UInt, Gui_Hwnd)
	WinGetPos,X,Y,W,H, ahk_id %Win_Hwnd%
	WinMove, ahk_id %Win_Hwnd%,,X,Y,W-1,H-1 ; Move
	WinMove, ahk_id %Win_Hwnd%,,X,Y,W,H ; And Move Back to Force Recalculation of Margins
	if Button
		ControlSend,,{Tab}{Tab}+{Tab}, ahk_id %Gui_Hwnd% ; Move to Button
	else
		ControlSend,,^{Home}, ahk_id %Gui_Hwnd% ; Unselect Text and Move to Top of Control
	DllCall("HideCaret", "Int", Gui_Hwnd)
	if ((Options ~= "i)p(?!\d)") or (!(Options ~= "i)p") and Button))
		while (ButtonPushed = -2)
			Sleep 50
	else if RegExMatch(Options, "i)p(\d+)", Match)
	{
		TimeEnd := A_TickCount + Match1
		while (A_TickCount < TimeEnd and ButtonPushed = -2)
			Sleep 50
		Gui_List.Delete(Gui_Label)
		Gui, Destroy
	}
	DetectHiddenWindows, %Setting_A_DetectHiddenWindows%
	SetWinDelay, %Setting_A_WinDelay%
	return ButtonPushed

	AllGuiSize:
		Resize_Gui_Hwnd := Gui_List[A_Gui]
		if Button
		{
			if (Button = 1)
			{
				EditWidth := A_GuiWidth - 20
				EditHeight := A_GuiHeight - 20 - (Adjust1 * 3)
				ButtonX := EditWidth / 2  - Adjust1
				ButtonY := EditHeight + 20 + (Adjust1/6)   
				GuiControl, Move, %Resize_Gui_Hwnd%, W%EditWidth% H%EditHeight%
				GuiControl, Move, %ScrollBox_Button1_Hwnd%, X%ButtonX% Y%ButtonY%
			}
			else
			{
				EditWidth := A_GuiWidth - 20
				EditHeight := A_GuiHeight - 20 - (Adjust1 * 3)
				Button1X := EditWidth / 4 - (Adjust1 * 2)
				Button2X := 3 * EditWidth / 4  - (Adjust1 * 1.5)
				ButtonY := EditHeight + 20 + (Adjust1/6) 
				GuiControl, Move, %Resize_Gui_Hwnd%, W%EditWidth% H%EditHeight%
				GuiControl, Move, %ScrollBox_Button1_Hwnd%, X%Button1X% Y%ButtonY%
				GuiControl, Move, %ScrollBox_Button2_Hwnd%, X%Button2X% Y%ButtonY%
			}
		}
		else
		{
			EditWidth := A_GuiWidth - 20
			EditHeight := A_GuiHeight - 20
			GuiControl, Move, %Resize_Gui_Hwnd%, W%EditWidth% H%EditHeight%
		}
	return

	AllGuiButtonOK:
		ButtonPushed := 1
		Gui, %A_Gui%:Destroy
		Gui_List.Delete(A_Gui)
	return

	AllGuiButtonYES:
		ButtonPushed := 1
		Gui, %A_Gui%:Destroy
		Gui_List.Delete(A_Gui)
	return

	AllGuiButtonNO:
		ButtonPushed := 0
		Gui, %A_Gui%:Destroy
		Gui_List.Delete(A_Gui)
	return

	AllGuiEscape:
	AllGuiClose:
		ButtonPushed := -1
		Gui, %A_Gui%:Destroy
		Gui_List.Delete(A_Gui)
	return

}

sort_by_length2(the_arr){ ; arrays
	;create a new array. Set an element's string length as its key and write to the new array  :clap: I feel clever. :crazy: 
	arr_temp:=[]
	for key, val in the_arr
	{
		this_len:= strlen(val)
		if (arr_temp[this_len]="")
			arr_temp[this_len]:=[]	; The fix
		arr_temp[this_len].push(val)
	}
	;~ return arr_temp
	out := []	; Optionally return a one-dimensional array instead.		
	for k, len_k in arr_temp 
		for l, str in len_k
			out.=str "`r`n" ;out.push(str) - culprit for increased slowness at high indices
	return RTrim(out, "`r" "`n")
}

SmallestFirst(the_arr) { ; loops
	loop, parse, the_arr, `n, `r
	{   (ttM<ttx:=StrLen(A_LoopField))?(ttM:=ttx)
		tLine%ttx% .= A_LoopField "`n"
	}

	loop, % ttM
	{	Last:=A_Index
		if tLine%A_Index%
			loop, parse, tline%A_Index%, `n
				if A_LoopField
					(Last<ttM)?(the_arr2.=A_LoopField "`r`n"):(the_arr2.=A_LoopField)
	}	return the_arr2 ;text_arr2:=StrSplit(the_arr2, "`n", "`r")
}

sort_by_length3(the_arr) { ; array & append"
	arr_temp:=[]
	for key, val in the_arr {
		this_len:=strlen(val)
		(arr_temp[this_len]="")?(arr_temp[this_len]:=[]):("")
		arr_temp[this_len].push(val)
	} for k, len_k in arr_temp
		for l, str in len_k        ; line below - amateur code (complicated, not too efficient)
	(k<arr_temp.MaxIndex()||l<len_k.MaxIndex())?(out_list.=str "`r`n"):(out_list.=str)
	return out_list ;StrSplit(out_list, "`n", "`r")
}

sort_by_length4(the_arr) {  ; append - array assisted1
	arr_temp:=[]
	for key, val in the_arr {
		this_len:=strlen(val)
	(arr_temp[this_len]="")?(arr_temp[this_len]:=[], arr_temp[this_len].push("")):("")
		arr_temp%this_len%.=val "`r`n"
	} for k in arr_temp
		out_list.=arr_temp%k%
	return RTrim(out_list, "`r" "`n") ;StrSplit(RTrim(out_list, "`r" "`n"), "`n", "`r")
}

ShortestFirst1(a1, a2, ShortestFirst) ; by Lexikos
{	if (StrLen(a1) != StrLen(a2))
		return (StrLen(a1) - StrLen(a2))
	return ShortestFirst*(-1)
}

ShortestFirst2(a,b){ ; untested, respecting alphabetical order
	return (d:=strlen(a)-strlen(b)) ? d : a > b ? 1 : a < b ? -1 : 0
}

ShortestFirst3(a1, a2, ShortestFirst) {	; untested, respecting order of appearance
	return (d := strlen(a1) - strlen(a2)) ? d : -ShortestFirst
}

ShortestFirst4(a,b){ ; untested, likely not to respect order of appearance
	return strlen(a)-strlen(b)
}

sort_by_length(the_arr){ ; original script with a bug
	;create a new array. Set an element's string length as its key and write to the new array  :clap: I feel clever. :crazy: 
	arr_temp:=[]
	for key, val in the_arr
	{
		this_len:= strlen(val)
		if (arr_temp[this_len]="") {
			ObjRawSet(arr_temp, this_len, val)			
		} else {
			arr_temp.InsertAt(this_len, val)			
		}
	}
	return arr_temp
}

f(ByRef arr){ ; append - array assisted2
	lens := []
	for k, ss in arr
		lens[l := strlen(ss)] := "", slen%l% .= ss "`r`n" 
	for l in lens
		t .= slen%l%
	return rtrim(t, "`n") ;strsplit(rtrim(t, "`r" "`n"), "`n", "`r" )
}

g(ByRef str){ ; append - array assisted3
	lens := []
	loop parse, str, `n, `r
		lens[l := strlen(A_LoopField)] := "", slen%l% .= A_LoopField "`r`n" 
	varsetcapacity(str, varsetcapacity(str)+2)
	for l in lens
		str .= slen%l%
	return rtrim(str, "`n") ;strsplit(rtrim(str, "`r" "`n"), "`n", "`r" )
}

sort_len(byref str, del := "`n", shortFirst:=true, omitEmpty:=true, init:=false){
	; source, see: 'c souce\sort_len.c'
	local k, i, raw, r
	static flProtect:=0x40, flAllocationType:=0x1000 ; PAGE_EXECUTE_READWRITE ; MEM_COMMIT	
	; binary code:
	static raw32:=strsplit("1398167381,9235585,2223702016,40996,615811840,164,2888084619,1711276032,251672707,247684,1048798720,3179548416,2231369731,3045330925,3338665987,3355714628,3338665984,9220,2499739648,46116,2311095552,608471495,2441350944,3338665987,3355714628,3338665984,9220,2499739648,46116,2311095552,254026820,642692,608487168,51204,604292864,0,3022296319,2231369728,264341952,634500,2382006528,2089488479,1099768868,608995600,609519912,281510688,2467289401,264387009,3389637267,606356537,968987407,254289020,3356082323,2215625348,4175,606358667,1552668985,2467242020,268122562,3255353491,271942671,3632857088,3239043203,3640066792,3800285833,4102295299,3338665986,3,3352267008,1,608996096,33194792,455,2215575552,4150,3338926723,1091,1204224000,4,71419648,0,276530447,1137115136,8,138921728,0,955,138528512,0,12223,3323904,3492347904,337921161,11960,3251644672,3229811432,608471297,48283928,472138889,539249803,9766029,1711276032,2378231567,1284178196,688858148,62980354,254288964,286195985,1109987072,1091637008,1074859792,1109987088,1091637024,1074859808,1109987104,1091637040,1074859824,1109987120,1091637056,1074859840,1109987136,1091637072,1074859856,1109987152,1091637088,1074859872,1109987168,1091637104,1074859888,2183729008,128,2155942159,251658240,8421393,688848896,36994,2165378816,144,2424312079,251658240,10519081,286195712,41089,2148601600,160,405044355,253064460,11567657,286195712,45185,2148601600,176,472138891,1144768297,344790052,1962510595,608471915,612141864,33129248,606362763,9897159,3338665984,37636,80150528,144,21138688,80167540,135,612141824,46301992,3338860931,33540,80150528,135,2334225408,3340772420,36868,1149960192,80159780,144,608471808,2416232232,0,28896645,3120562176,0,3351594255,3941444,3338665984,842540100,2298478592,608471495,2213579084,1153896416,6180,1153892352,3289124,3906011136,3766737143,267925767,3341403975,1149879177,1200439332,612141560,65585492,2298593411,3243779140,3984917472,1545880713,268387725,3347694148,3221346441,1149886249,4026621988,1210350729,1344554121,823150221,612141567,608471340,112660328,740588683,2357975857,40996,608487168,4294967068,1149855487,3062699044,0,2367620237,3071230220,3229967873,996548468,1947477060,29590398,2686749835,2365587456,210575108,28774209,1975551334,29372897,608487363,284,266044672,32645,612666112,160,405030019,1015244289,2215575672,1497,3247042305,2422645483,9225345,3677421568,1583077513,3351469407,807150660,3338665984,202908740,3204448256,50,337921223,50,1776933681,838860797,29393856,1091876623,1175730534,247170319,3308847104,246646287,2238054400,971142610,1954877381,3925868543,4294967161,740588681,807697547,337927305,2198862649,1588,337935499,941900939,210630713,4294966461,3343323135,941900937,1009009803,1108342841,608471495,608471868,2311586088,2336498756,29591808,2311555576,2335450180,2367956036,16792916,608471496,956336996,3598847938,2231369733,1347008,1149965685,4169339940,672435506,3766730777,46170928,64007951,2367693900,1418269188,1149857828,25887780,1814318217,4280550537,11805844,3229941760,1814318219,1881429131,239698959,2089484288,25781284,3263764361,337921163,740588683,1680100489,1149878273,2223704100,40996,2013564160,69485705,2304377997,2499748868,48164,2344649984,258221132,919172,608471808,17402676,18124224,1814316169,337921163,135808141,960562317,1150488518,1413178404,3054463012,258221148,3272130707,203785231,4253220864,478547726,2332033036,2235573372,2391019519,251658248,4286777015,42558977,210273295,1149960192,3071226916,3229811294,50299649,1711281293,255925385,811908,1186402048,67076868,289704294,1199836930,251658252,2198226615,2305164543,251924804,797572,1186402048,100631304,289704294,260312838,251658252,2198488759,2305165311,252186948,798597,1186402048,1149855244,129632785,956301312,256648316,58244,612139776,1685456456,1814316171,1612989443,740588683,337927307,1680098503,0,1881429129,1149880321,1149987876,1418416164,1149463588,2197906468,258347200,2213568623,286200002,1418326114,1418423332,1413178404,3664926756,1545880715,1881429131,740588681,337927305,1144635905,2020889636,874806411,1444722447,3489790089,1092389222,956383885,258045637,21372087,478766840,37915969,1349961017,1176286991,2305226753,1116553500,1992636675,481759039,1727529286,2369854601,3308848194,3071225462,4160833052,1092389222,956646029,253589189,21372087,478766840,105024833,209110329,1175762703,874791939,1091864934,337927307,1143227531,402778881,740580353,472153219,1451495423,2348810236,11019396,3229941760,108102671,2223702016,45092,264275200,696197,608471808,264275224,693124,759467264,614238976,160,2894713393,44068,608471296,608471852,608471320,273059092,472138889,472144955,2366653325,2467238444,264649153,3238576275,99714063,3162701824,44068,2249133568,1507,1076120715,2215627141,714,2198255375,2305163769,3011776259,251658250,2197964471,2305164025,2215576131,2614,71743247,1711536515,251937673,661892,1186402048,83460870,105089382,175408143,3071213568,4186114118,1133078021,1468272392,251658250,2198488759,2305165305,2232355395,2618,205960975,2305216393,129502275,956301312,256648316,65924,612139776,1551238728,1613003915,1344556171,874792135,0,1143227529,3154374401,40996,615287552,172,695144294,2198914947,20194372,2333131139,255075396,1005612817,1918379076,612142051,608471900,615287108,172,2084173825,2215597092,158,1178384143,2197949581,2218328512,44068,615287552,160,1300007270,260011520,2370190519,2305163788,2365607292,2352546120,44068,258438656,21904567,2089379537,1217200205,613169410,172,3071234166,3506523708,1300007270,55086336,2888076345,1979711488,1018629945,1724973390,5078153,956582029,11281548,578158592,1312601871,17154179,612645329,172,1300007270,252409344,21367991,1284073168,2483224645,44068,610009856,611091244,2232353044,4294966840,405042315,2888084619,3338665984,1582148,251658240,210630063,608471862,610044732,4286090552,956427139,1686507487,2298478599,11281580,1284177920,4119406628,606368907,3280145387,266287361,473734,2651097856,4000628869,2308506763,18097228,608471488,608471816,2550434592,69485705,2686747787,2365587456,76113924,613744420,188,2215624837,1977,337923211,3952684035,610044849,266044732,4294789765,613190655,176,91539845,2305217073,610044688,608471832,1958774064,2314613039,2333353052,2301633660,611093491,615811872,184,10310795,91542149,4280554633,29590486,3967278905,337927307,539247755,4280550537,12067988,1149960192,76097572,613744420,184,606356619,4280550537,12067988,3296788480,140,1583077513,3150142815,4294967295,673465543,0,807683271,50,3224470507,1210350723,2911309574,268435453,3163227831,40996,1727891712,3092710537,1,4294840041,608961535,3910241056,4294965876,539247755,14488717,2298478592,2298750036,2302944340,2499748868,46116,2311095552,2335450180,256123988,558724,608471808,609519908,609519876,604277060,3022296319,2231369728,608471488,609520416,629411652,2332033032,2301109316,2298750036,2499748868,46116,2311095552,444862401,2332033032,18097220,608451008,608471344,696454980,2332033026,2301633628,2635369927,16,2089410345,2056070180,956599792,1680112777,1150010249,1552495652,3489754148,1552663353,2467247140,24126532,289181179,673471625,2089417529,2089514020,2467242020,58205252,258221180,2015648950,1881431050,2089404217,2467262500,609485760,609499424,265783592,3255419539,606370947,3231125261,3280261153,114852879,1418395648,1413051428,2467262500,612121026,3230863212,2215625224,1726,673469579,807689355,2299519619,609520592,48808228,3766737143,970426627,265980368,4286969926,673479817,2088984180,2332108836,2369004612,2089484627,80158756,152,2667890432,0,10028231,1946157056,612139831,80151336,144,2533672704,0,9503943,2365587456,410321491,9438407,3338665984,38660,80150528,145,55807232,673479819,606356619,337927307,606356679,0,2082755721,1149892649,3900909604,48808196,2298593411,3245089860,1149829856,1149988900,76375076,612142008,2311520564,2338595964,18883708,2311586247,2339644540,2301371516,829957188,609520576,4010763876,4010763977,608469970,688849188,1418396172,286225444,1418396180,286226468,1418396180,3229819940,609499920,2345562732,2339382340,2306614356,2301371516,18097244,608451010,2371974184,1144586562,2089501732,1552626724,80158756,151,2466563840,0,9503943,1979711488,2265237307,0,8586439,3338665984,33028,1116536832,608450818,2333963844,3342083196,34564,2089484288,80158756,135,2164573952,0,539247755,941915275,807697465,673467529,606356617,874792075,539247753,1143227531,74679311,1149829120,4058591268,2348810230,11281580,3224436736,1175238415,1124895078,956416131,2314303429,11281580,2213085184,2348810235,2335712324,2369528924,3951296376,266287361,363907,615811328,164,2888084617,3338665984,1320004,2332033024,2334401652,3945276524,7769354,956427139,2337108959,3229982212,1150022260,2357919901,40996,2311061760,2332566596,2334139460,1149868036,1149961252,76354596,604277057,3156514047,2231369728,1585713088,2332033027,16817484,3943965772,3922866611,4294965237,337921163,2753868939,2332033024,11281580,1418395648,210581540,608471808,4290937128,54525951,10495116,479002624,688098192,2311061992,2332566596,2334139460,210341892,608471332,613744388,188,2215624837,761,337921163,50587689,2955189387,2231369728,608471515,2189758228,2332033026,2232951884,4018409417,2298478593,759467463,613714688,160,2888084617,2298478592,2335450180,10495108,479002624,273059192,472138889,1133371273,1003956496,253502556,3325673875,146838287,2558791617,2197815298,11281596,252575744,166534,608996096,264865088,104580,112660224,1711405443,2215576457,937,38188815,1711470979,251806601,247940,1186402048,66683652,71534950,61572111,3071213568,4186113606,1133078020,2391019270,251658243,2198357687,2305164793,2215577667,881,172406543,1711798659,252330889,199045,1186402048,1724745996,3087811465,7,1277455417,16417807,2088960000,1980123172,612107093,608996192,615287632,172,740574407,0,941900937,258397953,3347263855,281117456,740574339,608471809,1997606700,608451568,2346938968,2338071676,2302157892,11281580,4160815104,1411677241,10388495,1284177920,3071218724,3238086204,956416131,11281540,2305163264,2249148988,128,337931403,1178384143,3238128009,1245481318,956385421,11281548,1685454848,1312601871,2305222913,1217219132,613169410,172,3071233654,3909176892,1245481318,956516493,11281548,947257344,1312601871,2305222913,1217219132,613169412,172,3071222390,3229830716,971571461,11281540,2305163264,209078844,1175238415,337921027,1108117862,2888088715,50331648,20194396,2199135356,18359404,4265248015,3677487103,4294528233,2210411007,105391228,4276061967,3071279103,612141830,76113428,112762,1038680064,2315255807,615287790,172,2335575181,2335974484,52962372,10495116,3263234048,1073741823,2341477517,31992067,608471488,608471816,2416216864,2300841097,4278461508,12330132,3229941760,2223731572,45092,264275200,4294496388,65939967,611617075,612666132,160,337935499,2305217073,3001645076,2348810232,2334139476,824714316,610044864,649366820,0,8520903,3338665984,33540,80150528,129,29393664,1966274691,4051364323,834666495,3137339392,1,4293916905,4294949887,1726611455,2348810232,11281580,3224436736,1175238415,1124895078,956416131,2314303429,11281580,3588816896,2348810238,2334925948,823403612,213323712,210331206,29393730,4051158329,740588681,337927305,4294260713,3194879,45809664,3909091328,4294963093,4130457905,4294405353,608471551,4219791672,62455807,2298478592,4129155535,45678591,2298478592,4128369103,96141311,3909091328,4294964231,1210,4093503744,62586879,3909091328,4294964211,698,4092193024,29032447,3909091328,4294964191,1722,4090882304,112787455,2298478592,4123650511,96010239,2298478592,4122864079,79233023,2298478592,4122077647,28901375,2298478592,4121291215,112787455,2298478592,4244302285,1150025727,2089496612,1552624676,1418400804,80163876,130,609520384,2181351200,0,8455367,2197815296,1144586688,3665118244,740588681,337927305,4294610921,112895,3448307712,4294750185,375039,3448307712,4294747113,309503,3448307712,4294744041,243967,3448307712,4294740969,178431,3448307712,4294737897,264583679,4293972100,3531958015,4038951951,3271688191,2686749835,3909091328,4294963328,874792075,472145035,539247753,4294361321,608996351,608471840,610044724,608995612,608471332,4138199328,3375497215,337921223,0,4075405961,2348810234,3910935644,4294964877,2425393296,2425393296,2425393296",",")
	static raw64:=strsplit("1447122753,1413567809,1398167381,2834071880,1711276032,1207974275,2303249289,3330885079,265193796,223108,981689856,1568935680,1157627907,2215627141,852,2428160305,4278190081,18359444,2236088320,3314108864,54166543,3375431680,51386,613744384,280,1237353800,2215626633,2025,3367684401,4278190080,18359444,2303262720,1149847746,2236426276,3397652434,1224736775,1225803661,26250637,961085440,3498659010,1220711183,1225834627,2467284793,1287784896,88658057,200,1103444297,1237357327,2467285561,3221832896,2215625348,2391,3364326729,1224736768,2467284281,3476638146,146838287,1015287746,1090519049,252233206,177540,1170688256,0,28918016,1090519040,1991,3091267584,49,182081,3120562176,1,3578039625,4010763776,4010763977,3804317888,210585858,3523300375,838370113,252193039,252725289,286195985,1210650370,1210650400,1091637040,1108414224,1210650384,1210650432,1091637072,1108414240,1210650400,1210650464,1091637104,1108414256,2284392240,128,2424842511,251658240,255869201,255869457,10520617,688848896,45192,1091636992,1108414288,2284392272,192,3498584335,251658240,257966353,257966609,14714921,688848896,61576,1091636992,1108414320,2284392304,256,277358863,251658241,8421649,286195712,32898,2284392192,288,814229775,251658241,9470225,286195712,36994,2284392192,320,1351100687,251658241,10518801,286195712,41090,2284392192,352,1887971599,1090519041,254820749,11567377,286195712,45186,1153911040,197,3342925824,34564,3342925824,33284,561250304,2290468681,1,1090519040,12879815,0,3342925824,50306,0,3103163648,1,186,3326414592,611092812,3028894776,65572,2311162112,9446532,2303197184,266372088,1143227591,0,1076118727,50,3353923912,842277956,1291845632,4148755849,132154328,1192216633,1103178177,1149879177,1099783204,613190136,140,2198071489,2223571392,34852,65061120,2223634053,33828,4282813696,1170359311,826659377,1153534436,2303056009,1220618710,2303246337,1215308868,1284110337,2303219748,1215833156,1209026445,1612989577,263655236,4294950721,3979476991,4202255,738495809,264276297,1715668151,427081861,3359196262,3313724020,76366081,3230222636,1124382479,1975551334,29372903,29311429,2231369728,1148614125,2202132617,2204500422,251675452,281220,4093722880,2422649579,1156985157,2169041033,43204,1600019200,1096565085,1096696157,826655583,850936265,822083584,4251118034,258408447,17439,1217249329,344801537,3364440072,1393866511,1326725478,139035919,3459842048,138511887,2238054400,970683858,3951064526,611072902,2441285428,2332033028,1212163140,673467531,894733636,266877184,1149879623,1149976612,3896067108,2311406095,2370053188,2370633541,34052,21757952,3892399553,1091144516,2303201419,1129325644,956959885,797052881,2231369732,479020242,2199024914,3524866813,2201019011,255918786,2302792367,1284196563,2302949412,2223590618,38948,2357806080,37924,2492023808,32804,3523299328,610044228,76367976,1149847745,2336770084,613744392,280,1153467720,1747213451,613714756,128,613190468,148,612666188,152,121275407,2336751616,1212687436,2303000969,3800646680,613714244,128,1208782154,608995660,344803432,1150110803,2499739693,75812,3229960192,608996172,2492154984,32804,3800305408,1207959558,1210336395,692358477,210585864,479022121,277563401,1107594574,436505930,1103575372,1220645647,1612989499,3230863169,264308805,407172,218006272,103646735,1821048832,3984940068,79397903,256114688,4253222839,2302961153,981733120,1140850694,38778639,21073228,1258487171,1711277197,1108117830,102073359,256114688,2198095799,1147536381,33703049,104039439,256114688,2198226871,1147536637,67257481,102073359,256114688,2198357943,1147536893,100811913,100107279,256114688,2198489015,1147537405,134366345,98141455,256114688,1712080823,38046020,505866,2336489472,9446532,960757760,257696836,59524,612139776,1316357720,608996172,3224454512,422350155,610044748,3375449464,1725235528,74387267,3246604547,286212609,1224738116,1141948547,2284096571,1912602624,2626372834,33828,3623961600,614218564,140,9667599,2302738432,29393856,213323590,3355527495,1181140537,1984040073,3230220668,213323590,3355527495,210323046,1082999874,3325641729,256271222,1229393079,1181140993,1145179273,1140998285,1299629625,213323590,3355527495,210323046,1082999874,4030284035,256259955,1229393079,1181140993,1145179273,1090797709,561246265,213323590,96502599,969408841,2303092422,225853964,79105860,3355527239,76104806,1149978690,21319716,268518612,4294935361,4232479759,2303066111,4018753010,615811908,256,611093324,4135929144,41649167,2336489472,17835164,2235891712,1870991323,1157627908,2235940913,3515434450,73434127,2302935040,264253890,17439,481759044,3229829191,2211547393,3325624770,478757990,1105819467,2202136577,3597992425,1171753284,2937049649,1224247806,2336538625,2336236660,1094984812,2197941891,960561645,2525171694,1090519043,2302999689,2302157908,1091431415,1090635395,2198859321,877,1173391684,1216812171,8721549,1157627904,3748975237,611617608,3590408232,3310652233,3767092224,1208388420,1296239757,2499788801,75812,3229960192,67929103,54788096,2343627558,1162093676,2303586953,3029026031,65572,1821068288,3984930852,4278945039,3163291647,69668,1962902784,1727410437,2303013769,608472022,1958774068,611617575,3163244596,73764,266023168,17439,3712781129,3380955136,3623813748,29590344,3967278649,4293495116,18883732,2336751616,4280820812,18883732,2303459328,613744633,288,4294654697,4290658815,1224736767,673465543,0,874792135,50,2336791531,1211638860,1220609165,1210336393,3909126984,4294966337,760515908,1284196352,2302949412,1246766164,13964429,1275068416,1210340489,405050623,1207959553,2337063045,1145578580,1344556171,54166543,2370699264,38172,2302935040,1212687436,1344554121,1290373452,2303515273,4281869404,18359444,2236088320,1552633024,2336503844,1279796300,1344554123,51479567,2303459328,1145578564,941902985,1222281548,1344554121,608996168,613744424,280,1153467720,941902987,608471884,1418415176,2215596068,728,874794123,2311665988,2300670922,29524945,1238448449,13108423,1090519040,9045191,3338665984,34820,3748986880,1076122763,874796089,1221953869,673465481,608471372,1417940792,1140850690,874794121,4294604521,611093503,1955284036,3984801828,4001579265,4114170881,41911823,826605568,3163113700,63524,1418281984,2302949412,4102637031,1224116545,351002249,8658703,0,1140977027,2249188665,141,2336614537,2370340612,34076,2235891712,1222735040,673471491,3310652233,1224247552,1145441421,21824395,613744576,296,264275272,120196,3946513152,2210410935,106439804,4225664783,3071279103,76113415,112714,854130688,1224736764,673469579,2422652977,1162135369,0,80167168,7,33867520,0,79725384,13122888,3698655232,4294452457,1157204479,941905035,616336200,248,1156811080,2303063689,3431549414,1221132616,673465483,4115958603,3640739840,609519940,747457080,4290658736,1157627903,1090536843,21884969,613744576,296,264275272,4294793348,613190655,272,1155631429,941905035,1156655425,2231389443,2558857161,1157627904,628413061,835356996,3071231168,2202552068,3515417024,956416643,2302961350,3916909316,1106313537,1963059843,4130424283,4294772969,1418413311,2302949412,1224640992,21546889,1284196544,2302945316,1094198356,4294967230,1418414591,2370568429,2370349348,2336555788,692134916,3221310960,673486079,1207959553,2215624837,4294966465,613190468,272,609520452,3380954424,4238902287,4146724863,1173981505,824445955,2302830290,2481539860,838860796,213323730,210331223,3263383632,2010528001,4216318448,3224502271,3488218929,3103784955,2,4294590953,112895,938016768,3103784954,6,4294585833,375039,602472448,3103784954,4,4294580713,243967,266928128,1157627898,1076120713,608995652,4162709812,3191996415,4294967295,4294713577,4169745663,1290766669,941911179,3922102593,4294966285,1308526917,2337075081,3912770668,4294966269,1171622221,2303590025,3314109935,4294700265,1170813439,1424614449,973078526,1216614342,1728053239,2215629445,4294965047,99206401,2432696311,2425393296",",")
	; Functions:
	static pr:=DllCall("Kernel32.dll\GetProcAddress", "Ptr", DllCall("Kernel32.dll\GetModuleHandle", "Str", "MSVCRT.dll", "Ptr"), "AStr", "realloc", "Ptr")
	static fr:=DllCall("Kernel32.dll\GetProcAddress", "Ptr", DllCall("Kernel32.dll\GetModuleHandle", "Str", "MSVCRT.dll", "Ptr"), "AStr", "free", "Ptr")
	static cpy:=DllCall("Kernel32.dll\GetProcAddress", "Ptr", DllCall("Kernel32.dll\GetModuleHandle", "Str", "MSVCRT.dll", "Ptr"), "AStr", "memcpy", "Ptr")
	static bin:=sort_len("","","","",true) ; init.
	if init {
		if !(pr*fr*cpy)
			throw exception("Failed to retrieve function pointers: " pr "`t" fr "`t" cpt ".")
		bin:=DllCall("Kernel32.dll\VirtualAlloc", "Uptr",0, "Ptr", (raw:=A_PtrSize==4?raw32:raw64).length()*4, "Uint", flAllocationType, "Uint", flProtect, "Ptr")
		for k, i in raw
			NumPut(i,bin+(k-1)*4,"Int")
		raw32:="",raw64:=""
		return bin
	}
	;return DllCall(bin, "ptr", &str, "ptr", &del, "int", shortFirst, "uint", strlen(del), "ptr", pr,"ptr", fr,"ptr", cpy, "ptr", registercallback("mb", "f cdecl"), "cdecl") ; db
	r:=DllCall(bin, "ptr", &str, "ptr", &del, "int", shortFirst, "uint", strlen(del), "int", omitEmpty, "ptr", pr,"ptr", fr,"ptr", cpy, "cdecl")
	if omitEmpty && r
		varsetcapacity(str, -1)
	return r != -1 ; 0 on fail, 1 on success
}

^Esc::
WinClose, ahk_class Notepad
ExitApp
To do: Sorting by numbers. (Not sure if it's doable with sort_len() function.)

P.s.: @Helgef: How fast is sort_len() in c in comparison with other c sorting function?
Last edited by rommmcek on 02 Dec 2017, 15:57, edited 3 times in total.
Helgef
Posts: 3303
Joined: 17 Jul 2016, 01:02
Contact:

Re: one-loop sorting of strings by their length

02 Dec 2017, 13:39

Note, WinWaitActive, Untitled ahk_class Notepad,, 1 will not work on non-english OS.
To do: Sorting by numbers. (Not sure if it's doable with sort_len() function.)

P.s.: @Helgef: How fast is sort_len() in c in comparison with other c sorting function?
sort_len sorts only by string length, it cannot do anything else. I have not compared it to anything.

Cheers :xmas:
rommmcek
Posts: 391
Joined: 15 Aug 2014, 15:18

Re: one-loop sorting of strings by their length

02 Dec 2017, 15:03

Fixed, Thanks!!!
dashunbaba
Posts: 3
Joined: 20 Oct 2017, 05:37

Re: one-loop sorting of strings by their length

05 Mar 2018, 10:53

Wonderful. Thanks! Will fix my app with this updated function :-)
Helgef wrote:You have the right idea, but need to make a small fix,

Code: Select all

sort_by_length(the_arr){
	;create a new array. Set an element's string length as its key and write to the new array  :clap: I feel clever. :crazy: 
	arr_temp:=[]
	for key, val in the_arr
	{
		this_len:= strlen(val)
		if (arr_temp[this_len]="")
			arr_temp[this_len]:=[]	; The fix
		arr_temp[this_len].push(val)			
	}
	return arr_temp
	/*
	out := []				; Optionally return a one-dimensional array instead.		
	for k, len_k in arr_temp
		for l, str in len_k
			out.push(str)
	return out
	*/
}
Cheers.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 27 guests