﻿id	summary	reporter	owner	description	type	status	milestone	component	version	severity	resolution	keywords	cc
2554	UDF - GuiTreeView.au3 - _GUICtrlTreeView_Sort - proposal	mlipok	guinness	"now is:

{{{
Func _GUICtrlTreeView_Sort($hWnd)
	If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)

	Local $hItem, $a_tree
	For $i = 0 To _GUICtrlTreeView_GetCount($hWnd)
		If $i == 0 Then
			$hItem = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_CHILD, $TVI_ROOT, 0, ""wparam"", ""handle"", ""handle"")
		Else
			$hItem = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_NEXT, $hItem, 0, ""wparam"", ""handle"", ""handle"")
		EndIf
		If IsArray($a_tree) Then
			ReDim $a_tree[UBound($a_tree) + 1]
		Else
			Dim $a_tree[1]
		EndIf
		$a_tree[UBound($a_tree) - 1] = $hItem
	Next
	If IsArray($a_tree) Then
		Local $hChild, $i_Recursive = 1
		For $i = 0 To UBound($a_tree) - 1
			_SendMessage($hWnd, $TVM_SORTCHILDREN, $i_Recursive, $a_tree[$i], 0, ""wparam"", ""handle"") ; sort the items in root
			Do ; sort all the children
				$hChild = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_CHILD, $hItem, 0, ""wparam"", ""handle"", ""handle"")
				If $hChild > 0 Then
					_SendMessage($hWnd, $TVM_SORTCHILDREN, $i_Recursive, $hChild, 0, ""wparam"", ""handle"")
				EndIf
				$hItem = $hChild
			Until $hItem = 0x00000000
		Next
	EndIf
EndFunc   ;==>_GUICtrlTreeView_Sort
}}}

I removed depracted Dim declaration,

By the way I removed it from the inside:
{{{
For $ i = 0 This _GUICtrlTreeView_GetCount ($ hWnd)
....
Next
}}}

By the way a little sped up.

I checked on a modified example from the documentation:

{{{
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Global $hImage, $hStateImage

Func _GUICtrlTreeView_Sort_new($hWnd)
	If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)

	Local $hItem, $vItemCount = _GUICtrlTreeView_GetCount($hWnd)
	Local $a_tree[$vItemCount]
	For $i = 0 To $vItemCount -1
		If $i == 0 Then
			$hItem = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_CHILD, $TVI_ROOT, 0, ""wparam"", ""handle"", ""handle"")
		Else
			$hItem = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_NEXT, $hItem, 0, ""wparam"", ""handle"", ""handle"")
		EndIf
		$a_tree[$i] = $hItem
	Next
	If IsArray($a_tree) Then
		Local $hChild, $i_Recursive = 1
		For $i = 0 To UBound($a_tree) - 1
			_SendMessage($hWnd, $TVM_SORTCHILDREN, $i_Recursive, $a_tree[$i], 0, ""wparam"", ""handle"") ; sort the items in root
			Do ; sort all the children
				$hChild = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_CHILD, $hItem, 0, ""wparam"", ""handle"", ""handle"")
				If $hChild > 0 Then
					_SendMessage($hWnd, $TVM_SORTCHILDREN, $i_Recursive, $hChild, 0, ""wparam"", ""handle"")
				EndIf
				$hItem = $hChild
			Until $hItem = 0x00000000
		Next
	EndIf
EndFunc   ;==>_GUICtrlTreeView_Sort

Example()

Func Example()
	Local $hItem[10], $iX = 9, $iY = 29, $hTreeView
	Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)

	GUICreate(""TreeView Sort"", 400, 300)

	$hTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
	GUISetState()

	_GUICtrlTreeView_BeginUpdate($hTreeView)
	For $x = 0 To 9
		$hItem[$x] = GUICtrlCreateTreeViewItem(StringFormat(""[%02d] New Item"", $iX), $hTreeView)
		$iX -= 1
		For $y = 1 To 3
			GUICtrlCreateTreeViewItem(StringFormat(""[%02d] New Child"", $iY), $hItem[$x])
			$iY -= 1
		Next
	Next
	_GUICtrlTreeView_Expand($hTreeView)
	_GUICtrlTreeView_EndUpdate($hTreeView)

	MsgBox($MB_SYSTEMMODAL, ""Information"", ""Sort"")
	_GUICtrlTreeView_Sort_new($hTreeView)
	_GUICtrlTreeView_SelectItem($hTreeView, $hItem[9])

	; Loop until user exits
	Do
	Until GUIGetMsg() = $GUI_EVENT_CLOSE
	GUIDelete()
EndFunc   ;==>Example
}}}

_GUICtrlTreeView_Sort_new($hWnd) is a modified _GUICtrlTreeView_Sort($hWnd)

Please consider changing the standard UDF.
"	Feature Request	closed	3.3.9.25	Standard UDFs		None	Completed		
