[Function] - Sort() - A Sort Function That Uses ListView Control! (Easily Sort Variables\Objects)

Post your working scripts, libraries and tools for AHK v1.1 and older
User
Posts: 407
Joined: 26 Jun 2017, 08:12

[Function] - Sort() - A Sort Function That Uses ListView Control! (Easily Sort Variables\Objects)

02 Oct 2018, 17:30

This function relies on ListView control!

Sort Options such as Sort, SortDesc, Float, Integer, Text, Case, Logical, etc, can be used in "Opt" parameter!

Easily Sort Variables\Objects:

Code: Select all

	;uncomment the "Local" line inside the function if you use AutoHotKey v1.1.27+
	;Force all variables in the function to be "Local" vars (with no exceptions)
	;"Force-local" mode (only supported on AutoHotKey v1.1.27+)
	;the function was tested in AutoHotKey 1.1.23.05

VarA := "AAA`r`nCCC`r`nBBB"
VarB := "BBB`r`nAAA`r`nCCC"
VarC := "CCC`r`nBBB`r`nAAA"

sort([VarB, VarC, VarA])		;Listview "Sort" option is used by default (Immediately sorts the column in ascending order)

out := ""
loop, % Sort("GetIndex")
out .= Sort("Get", a_index) "`r`n`r`n"

msgbox, % Out

;___________

	;Test := sort([VarB, VarC, VarA])	;(Alternative Method), the function returns an array (I prefer the above method!)

	;out := ""
	;loop, % Test.Length()
	;out .= Test[a_index] "`r`n`r`n"

	;msgbox, % Out

;____________

	;All listview col options, such as, SortDesc, Float, Integer, Text, Case, Logical, etc, can be used in "Opt" parameter:
	;https://autohotkey.com/docs/commands/ListView.htm#ColOptions

sort([VarB, VarC, VarA], "SortDesc")	;Listview "SortDesc" option (Immediately sorts the column in descending order.)

out := ""
loop, % Sort("GetIndex")
out .= Sort("Get", a_index) "`r`n`r`n"

msgbox, % Out

;____________

T1 := "T1111111`r`nYYYY`r`nOOOOOO"
T2 := "T2222`r`nZZZZ`r`nOOOOOO"
T3 := "T33`r`nXXXX`r`nOOOOOO"

sort([T2, T3, T1], "Text Logical")	;"Logical", the string "T1111111" will be considered greater than "T2222" and "T2222" greater than "T33"

out := ""
loop, % Sort("GetIndex")
out .= Sort("Get", a_index) "`r`n`r`n"

msgbox, % Out

;____________


exitapp


Sort(Object, Opt := "")		;________________________ Sort(Function) v1.0 ________________________
{

;Local	;uncomment this line if you use AutoHotKey v1.1.27+
	;Force all variables in this function to be "Local" vars (with no exceptions)
	;"Force-local" mode (only supported on AutoHotKey v1.1.27+)
	;this function was tested in AutoHotKey 1.1.23.05

	;Local AnyVariable	;(Not Recommended) this line would switch the function mode from "assume-Local" to "assume-Global" (makes all variables global by default)


Static Result := []

if (Object = "Get")
return, Result[Opt]

if (Object = "GetIndex")
return, Result.Length()

Options := "Sort " Opt

gui, SortFunctionGui:Default
gui, destroy

gui, add, listview, , Values

	loop, % Object.Length()
	{
	LV_Add(, object[a_index])
	}

LV_ModifyCol(1, Options)

	Result := []
	loop, % LV_GetCount()	;returns total numbers of rows
	{
	LV_GetText(TempString, a_index, 1)

	Result[a_index] := TempString
	}

	;gui, show

gui, destroy

return, Result

}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 85 guests