Function Reference


Sorts a list-view control (limited)

#include <GuiListView.au3>
_GUICtrlListView_SimpleSort ( $hWnd, ByRef $vSortSense, $iCol [, $bToggleSense = True] )


$hWnd Handle to the control
$vSortSense Sets the defined sort order
    True - Sort Descending
    False - Sort Ascending
Use a simple variable for a single column ListView
Use an array for multicolumn ListViews:
    $aArray[0] - Order for first Column
    $aArray[1] - Order for second Column
    $aArray[n] - Order for last Column
$iCol Column number
$bToggleSense [optional] Toggle sort sense
    True - Toggle sort sense after sort (default)
    False - Sort sense unchanged after sort

Return Value



This is a basic sort fuction, for advanced sort see GUICtrlRegisterListViewSort().

Toggling sort sense is normal behaviour when sorting is initiated by a click on the column header.
The user can prevent this toggle by setting $bToggleSense to False when calling the function from elsewhere.

If the function is called from within several functions and the sort sense is to be toggled, it is important that $vSortSense be Global in scope.
Otherwise the sense will be reset each time that $vSortSense is created and toggling will be ineffective.


#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

Global $g_idListView, $g_bSortSense = True ; Set initial ascending sort


Func Example()
        GUICreate("ListView SimpleSort (v" & @AutoItVersion & ")", 300, 300)
        $g_idListView = GUICtrlCreateListView("Item", 10, 10, 280, 240)
        _GUICtrlListView_SetColumnWidth($g_idListView, 0, 260)
        Local $idButton_Add = GUICtrlCreateButton("Add Item", 10, 260, 80, 30)
        Local $idButton_Set = GUICtrlCreateButton("Reset Ascending Sort Order", 110, 260, 180, 30)


        GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

        While 1
                Switch GUIGetMsg()
                        Case $GUI_EVENT_CLOSE
                        Case $idButton_Add
                                Local $sData = Chr(Random(65, 90, 1))
                                GUICtrlCreateListViewItem($sData, $g_idListView)
                                _GUICtrlListView_SimpleSort($g_idListView, $g_bSortSense, 0, False) ; Prevent sort direction toggling for next insertion
                        Case $idButton_Set
                                $g_bSortSense = False ; Reset for ascending sort when next sorted
EndFunc   ;==>Example

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
        #forceref $hWnd, $iMsg, $wParam

        Local $hWndListView = $g_idListView
        If Not IsHWnd($g_idListView) Then $hWndListView = GUICtrlGetHandle($g_idListView)
        Local $tNMHDR = DllStructCreate($tagNMLISTVIEW, $lParam)
        Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
        Local $iCode = DllStructGetData($tNMHDR, "Code")
        Switch $hWndFrom
                Case $hWndListView
                        Switch $iCode
                                Case $LVN_COLUMNCLICK ; A column was clicked
                                        _GUICtrlListView_SimpleSort($hWndListView, $g_bSortSense, DllStructGetData($tNMHDR, "SubItem")) ; Sort direction for next sort toggled by default
        Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY