A better example that sorts a list view by the column that is clicked on that column's header. The column sorts acsending and decending with each click.
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1) ; 1 = enable.
Opt("GUICloseOnESC", 1) ; 1 = Send the $GUI_EVENT_CLOSE message when ESC is pressed (default).
Opt('MustDeclareVars', 1)
Global $hListView
_Example1()
While 1
Sleep(100)
WEnd
Func _Example1()
;Local $hImage
Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
GUICreate("ListView Sort", 300, 400)
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseGUI")
$hListView = GUICtrlCreateListView("Column1|Column2|Column3|Col4", 10, 10, 280, 380, -1, $iExWindowStyle)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $iExListViewStyle)
GUICtrlSetOnEvent($hListView, "sort")
_AddRow($hListView, "10/20/2001|2|7|10/14/2012")
_AddRow($hListView, "12/15/2000|1|8|5/22/2012")
_AddRow($hListView, "04/05/2004|----|----|----")
_AddRow($hListView, "1/22/2004|4|6|1/4/2012")
_AddRow($hListView, "11/05/2005|5|3|10/12/2012")
_AddRow($hListView, "8/15/2004|6|2|10/10/2010")
_AddRow($hListView, "10/05/2008|7||11/11/2011")
_AddRow($hListView, "11/05/2012|8|4|05/15/2005")
_AddRow($hListView, "12/05/2012|9|5|1/21/2004")
GUISetState()
_GUICtrlListView_RegisterSortCallBack($hListView)
EndFunc ;==>_Example1
Func sort()
Local $iCol = GUICtrlGetState($hListView)
Local $iRowCount = _GUICtrlListView_GetItemCount(ControlGetHandle("ListView Sort", "", $hListView))
; ----- If the column clicked contains date formated "MM/DD/YYYY" in the first, middle, or last
; row, then convert to date format "YYYY/MM/DD" ----
If StringRegExp(_GUICtrlListView_GetItemText($hListView, 0, $iCol), "(d{1,2})[/.-](d{1,2})[/.-](d{4})") Or _
StringRegExp(_GUICtrlListView_GetItemText($hListView, $iRowCount - 1, $iCol), "(d{1,2})[/.-](d{1,2})[/.-](d{4})") Or _
StringRegExp(_GUICtrlListView_GetItemText($hListView, Int($iRowCount / 2), $iCol), "(d{1,2})[/.-](d{1,2})[/.-](d{4})") Then
Local $aItem2D[$iRowCount][2], $aTemp
For $i = 0 To $iRowCount - 1
$aItem2D[$i][0] = _GUICtrlListView_GetItemText($hListView, $i, $iCol)
If StringRegExp($aItem2D[$i][0], "(d{1,2})[/.-](d{1,2})[/.-](d{4})") Then
$aTemp = StringRegExp($aItem2D[$i][0], "(d+)", 3)
$aItem2D[$i][1] = StringFormat("%4d/%02d/%02d", $aTemp[2], $aTemp[0], $aTemp[1])
_GUICtrlListView_SetItem($hListView, $aItem2D[$i][1], $i, $iCol)
Else
$aItem2D[$i][1] = $aItem2D[$i][0]
_GUICtrlListView_SetItem($hListView, $aItem2D[$i][1], $i, $iCol)
EndIf
;ConsoleWrite($aItem2D[$i][0] & " " & $aItem2D[$i][1] & @LF)
Next
;ConsoleWrite(@LF)
; -------- Sort all columns ----------
_GUICtrlListView_SortItems($hListView, $iCol)
; -------- Convert date format "YYYY/MM/DD" back to the original format --------
For $i = 0 To $iRowCount - 1
For $j = 0 To $iRowCount - 1
If StringCompare(_GUICtrlListView_GetItemText($hListView, $i, $iCol), $aItem2D[$j][1]) = 0 Then
_GUICtrlListView_SetItem($hListView, $aItem2D[$j][0], $i, $iCol)
ExitLoop 1
EndIf
Next
Next
Else
_GUICtrlListView_SortItems($hListView, $iCol)
EndIf
EndFunc ;==>sort
Func _AddRow($hWnd, $sItem)
Local $aItem = StringSplit($sItem, "|"), $iColWidth
Local $iIndex = _GUICtrlListView_AddItem($hWnd, $aItem[1], -1, _GUICtrlListView_GetItemCount($hWnd))
_GUICtrlListView_SetColumnWidth($hWnd, 0, $LVSCW_AUTOSIZE_USEHEADER)
For $x = 2 To $aItem[0]
_GUICtrlListView_AddSubItem($hWnd, $iIndex, $aItem[$x], $x - 1)
_GUICtrlListView_SetColumnWidth($hWnd, $x - 1, $LVSCW_AUTOSIZE)
$iColWidth = _GUICtrlListView_GetColumnWidth($hWnd, $x - 1)
_GUICtrlListView_SetColumnWidth($hWnd, $x - 1, $LVSCW_AUTOSIZE_USEHEADER)
If $iColWidth > _GUICtrlListView_GetColumnWidth($hWnd, $x - 1) Then
_GUICtrlListView_SetColumnWidth($hWnd, $x - 1, $LVSCW_AUTOSIZE)
EndIf
Next
EndFunc ;==>_AddRow
Func CloseGUI()
_GUICtrlListView_UnRegisterSortCallBack($hListView)
GUIDelete()
Exit
EndFunc ;==>CloseGUI