Sign in to follow this  
Followers 0
rasim

Sort ListView items

22 posts in this topic

#1 ·  Posted (edited)

Hi! This example showed, how can sorting ListView items.

Waiting for yours comments and suggestions.

:)

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <Array.au3>

$Gui = GUICreate("Test", 300, 200)

$hListView = _GUICtrlListView_Create($GUI, "Items|SubItems1|SubItems2", 20, 15, 260, 170, BitOR($LVS_EDITLABELS, $LVS_REPORT), $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_SUBITEMIMAGES)

$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 3)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 11)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 22)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 33)

_GUICtrlListView_SetImageList($hListView, $hImage, 1)

_GUICtrlListView_AddItem($hListView, "Item1",0)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",1)
_GUICtrlListView_AddItem($hListView, "Item4",3)

_GUICtrlListView_AddSubItem ($hListView, 0,'44', 1, 1)
_GUICtrlListView_AddSubItem ($hListView, 1,'22', 1, 0)
_GUICtrlListView_AddSubItem ($hListView, 2,'11', 1, 3)
_GUICtrlListView_AddSubItem ($hListView, 3,'33', 1, 2)

_GUICtrlListView_AddSubItem ($hListView, 0,'New', 2, 2)
_GUICtrlListView_AddSubItem ($hListView, 1,'Page', 2, 3)
_GUICtrlListView_AddSubItem ($hListView, 2,'Sys', 2, 0)
_GUICtrlListView_AddSubItem ($hListView, 3,'Device', 2, 1)

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
WEnd
;===============================================================================
;
; Function Name:    _ListView_Sort()
; Description:      Sorting ListView items when column click
; Parameter(s):     $cIndex - Column index
; Return Value(s):  None
; Requirement(s):   AutoIt 3.2.12.0 and above
; Author(s):        R.Gilman (a.k.a rasim)
;
;================================================================================
Func _ListView_Sort($cIndex = 0)
    Local $iColumnsCount, $iDimension, $iItemsCount, $aItemsTemp, $aItemsText, $iCurPos, $iImgSummand, $i, $j
    
    $iColumnsCount = _GUICtrlListView_GetColumnCount($hListView)
    
    $iDimension = $iColumnsCount * 2
    
    $iItemsCount = _GUICtrlListView_GetItemCount($hListView)
    
    Local $aItemsTemp[1][$iDimension]
    
    For $i = 0 To $iItemsCount - 1
        $aItemsTemp[0][0] += 1
        ReDim $aItemsTemp[$aItemsTemp[0][0] + 1][$iDimension]
        
        $aItemsText = _GUICtrlListView_GetItemTextArray($hListView, $i)
        $iImgSummand = $aItemsText[0] - 1
        
        For $j = 1 To $aItemsText[0]
            $aItemsTemp[$aItemsTemp[0][0]][$j - 1] = $aItemsText[$j]
            $aItemsTemp[$aItemsTemp[0][0]][$j + $iImgSummand] = _GUICtrlListView_GetItemImage($hListView, $i, $j - 1)
        Next
    Next
    
    $iCurPos = $aItemsTemp[1][$cIndex]
    _ArraySort($aItemsTemp, 0, 1, 0, $cIndex)
    If StringInStr($iCurPos, $aItemsTemp[1][$cIndex]) Then _ArraySort($aItemsTemp, 1, 1, 0, $cIndex)
    
    For $i = 1 To $aItemsTemp[0][0]
        For $j = 1 To $iColumnsCount
            _GUICtrlListView_SetItemText($hListView, $i - 1, $aItemsTemp[$i][$j - 1], $j - 1)
            _GUICtrlListView_SetItemImage($hListView, $i - 1, $aItemsTemp[$i][$j + $iImgSummand], $j - 1)
        Next
    Next
EndFunc
;================================================================================

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
    Case $hWndListView
        Switch $iCode
        Case $LVN_COLUMNCLICK
            Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
            Local $ColumnIndex = DllStructGetData($tInfo, "SubItem")
            _ListView_Sort($ColumnIndex)
        EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
    EndFunc

EDIT: Changed _ArraySort() function param`s.

Edited by rasim

Share this post


Link to post
Share on other sites



Cool but you do know that _GUICtrlListview_SimpleSort exists already, and thus you just have to check for a click on a column and then sort that column. But well I guess if you can re-invent the wheel have at it :)

Share this post


Link to post
Share on other sites

Cool but you do know that _GUICtrlListview_SimpleSort exists already, and thus you just have to check for a click on a column and then sort that column. But well I guess if you can re-invent the wheel have at it :)

Yes, i know about this function, but this function do not sorting ListView items images :)

Share this post


Link to post
Share on other sites

If you are using the new _GUICtrlListView_RegisterSortCallBack in 3.2.11.0 you can sort with images, but it seams you can't use that new function togethet with WM_NOTIFY. So I think your solution looks great !

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for comments guys! :)

Edited by rasim

Share this post


Link to post
Share on other sites

I added some extra items only in column 1, then the sort doesn't work for the other columns.

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <Array.au3>

$Gui = GUICreate("Test", 300, 200)

$hListView = _GUICtrlListView_Create($GUI, "Items|SubItems1|SubItems2", 20, 15, 260, 170, BitOR($LVS_EDITLABELS, $LVS_REPORT), $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_SUBITEMIMAGES)

$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 3)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 11)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 22)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 33)

_GUICtrlListView_SetImageList($hListView, $hImage, 1)

_GUICtrlListView_AddItem($hListView, "Item1",1)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item1",1)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item1",1)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item1",1)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)


_GUICtrlListView_AddSubItem ($hListView, 0,'44', 1, 1)
_GUICtrlListView_AddSubItem ($hListView, 1,'22', 1, 0)
_GUICtrlListView_AddSubItem ($hListView, 2,'11', 1, 3)
_GUICtrlListView_AddSubItem ($hListView, 3,'33', 1, 2)

_GUICtrlListView_AddSubItem ($hListView, 0,'New', 2, 2)
_GUICtrlListView_AddSubItem ($hListView, 1,'Page', 2, 3)
_GUICtrlListView_AddSubItem ($hListView, 2,'Sys', 2, 0)
_GUICtrlListView_AddSubItem ($hListView, 3,'Device', 2, 1)

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
WEnd
;===============================================================================
;
; Function Name:    _ListView_Sort()
; Description:    Sorting ListView items when column click
; Parameter(s):  $cIndex - Column index
; Return Value(s):  None
; Requirement(s):   AutoIt 3.2.10.0
; Author(s):        R.Gilman (a.k.a rasim)
;
;================================================================================
Func _ListView_Sort($cIndex = 0)
    Local $iColumnsCount, $iDimension, $iItemsCount, $aItemsTemp, $aItemsText, $iCurPos, $iImgSummand, $i, $j
    
    $iColumnsCount = _GUICtrlListView_GetColumnCount($hListView)
    
    $iDimension = $iColumnsCount * 2
    
    $iItemsCount = _GUICtrlListView_GetItemCount($hListView)
    
    Local $aItemsTemp[1][$iDimension]
    
    For $i = 0 To $iItemsCount - 1
        $aItemsTemp[0][0] += 1
        ReDim $aItemsTemp[$aItemsTemp[0][0] + 1][$iDimension]
        
        $aItemsText = _GUICtrlListView_GetItemTextArray($hListView, $i)
        $iImgSummand = $aItemsText[0] - 1
        
        For $j = 1 To $aItemsText[0]
            $aItemsTemp[$aItemsTemp[0][0]][$j - 1] = $aItemsText[$j]
            $aItemsTemp[$aItemsTemp[0][0]][$j + $iImgSummand] = _GUICtrlListView_GetItemImage($hListView, $i, $j - 1)
        Next
    Next
    
    $iCurPos = $aItemsTemp[1][$cIndex]
    _ArraySort($aItemsTemp, 0, 1, 0, $iDimension, $cIndex)
    If StringInStr($iCurPos, $aItemsTemp[1][$cIndex]) Then _ArraySort($aItemsTemp, 1, 1, 0, $iDimension, $cIndex)
    
    For $i = 1 To $aItemsTemp[0][0]
        For $j = 1 To $iColumnsCount
            _GUICtrlListView_SetItemText($hListView, $i - 1, $aItemsTemp[$i][$j - 1], $j - 1)
            _GUICtrlListView_SetItemImage($hListView, $i - 1, $aItemsTemp[$i][$j + $iImgSummand], $j - 1)
        Next
    Next
EndFunc
;================================================================================

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
    Case $hWndListView
        Switch $iCode
        Case $LVN_COLUMNCLICK
            Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
            Local $ColumnIndex = DllStructGetData($tInfo, "SubItem")
            _ListView_Sort($ColumnIndex)
        EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
    EndFunc

Share this post


Link to post
Share on other sites

Wooltown

Hey! You good tester! :P

Function work, but slowly if ListView contains many items.

Here is example from GaryFrost, work little faster. :)

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

Dim $nCurCol = -1
Dim $nSortDir = 1
Dim $bSet = 0
Dim $nCol = -1

$Gui = GUICreate("Test", 300, 200)

$hListView = GUICtrlCreateListView("Items|SubItems", 2, 2, 296, 196, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))

$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 1)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 11)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 22)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 33)

_GUICtrlListView_SetImageList($hListView, $hImage, 1)

$ListItem1 = _GUICtrlListView_AddItem($hListView, "Item1", 0)
_GUICtrlListView_SetItemParam($hListView, $ListItem1, _GUICtrlListView_GetItemCount($hListView) + 9999)
$ListItem2 = _GUICtrlListView_AddItem($hListView, "Item2", 1)
_GUICtrlListView_SetItemParam($hListView, $ListItem2, _GUICtrlListView_GetItemCount($hListView) + 9999)
_GUICtrlListView_AddItem($hListView, "Item1",1)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item1",1)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item1",1)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item1",1)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item1",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item3",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item5",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item6",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)
_GUICtrlListView_AddItem($hListView, "Item4",3)

_GUICtrlListView_AddSubItem ($hListView, 0,'44', 1, 1)
_GUICtrlListView_AddSubItem ($hListView, 1,'22', 1, 0)
_GUICtrlListView_AddSubItem ($hListView, 2,'11', 1, 3)
_GUICtrlListView_AddSubItem ($hListView, 3,'33', 1, 2)

_GUICtrlListView_AddSubItem ($hListView, 0,'New', 2, 2)
_GUICtrlListView_AddSubItem ($hListView, 1,'Page', 2, 3)
_GUICtrlListView_AddSubItem ($hListView, 2,'Sys', 2, 0)
_GUICtrlListView_AddSubItem ($hListView, 3,'Device', 2, 1)

GUICtrlRegisterListViewSort($hListView, "LVSort") ; Register the function "LVSort" for the sorting callback

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hListView
            $bSet = 0
            $nCurCol = $nCol
            $SendMsg = GUICtrlSendMsg($hListView, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($hListView), 0)
            DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($hListView), "int", 0, "int", 1)
    EndSwitch
WEnd

; Our sorting callback funtion
Func LVSort($hWnd, $nItem1, $nItem2, $nColumn)
    Local $nSort
    Local $tInfo = DllStructCreate($tagLVFINDINFO)
    DllStructSetData($tInfo, "Flags", $LVFI_PARAM)
    
    ; Switch the sorting direction
    If $nColumn = $nCurCol Then
        If Not $bSet Then
            $nSortDir = $nSortDir * - 1
            $bSet = 1
        EndIf
    Else
        $nSortDir = 1
    EndIf
    $nCol = $nColumn
    
    DllStructSetData($tInfo, "Param", $nItem1)
    $val1 = _GUICtrlListView_FindItem($hWnd, -1, $tInfo)
    DllStructSetData($tInfo, "Param", $nItem2)
    $val2 = _GUICtrlListView_FindItem($hWnd, -1, $tInfo)
    $val1 = _GUICtrlListView_GetItemText($hWnd, $val1, $nColumn)
    $val2 = _GUICtrlListView_GetItemText($hWnd, $val2, $nColumn)
    
    $nResult = 0 ; No change of item1 and item2 positions
    
    If $val1 < $val2 Then
        $nResult = -1 ; Put item2 before item1
    ElseIf $val1 > $val2 Then
        $nResult = 1 ; Put item2 behind item1
    EndIf

    $nResult = $nResult * $nSortDir
    
    Return $nResult
EndFunc   ;==>LVSort

Edit: If possible don`t use Array.au3 UDF, because it slowed script. :(

Share this post


Link to post
Share on other sites

It works nice and a lot faster, perhaps someone can do an even faster sort algoritm, but I like your work and implement the sort function.

Share this post


Link to post
Share on other sites

hi,

i try to sort ListView items with images (thank's it work) ... and backgrounds color (some items lines have specifc color)

can you help me ?

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Copy pasted your sample & this is what I get: TESTED WITH : AutoIt Version: 3.2.12.0

(8,145) : WARNING: $WS_EX_CLIENTEDGE: possibly used before declaration.
$hListView = _GUICtrlListView_Create($GUI, "Items|SubItems1|SubItems2", 20, 15, 260, 170, BitOR($LVS_EDITLABELS, $LVS_REPORT), $WS_EX_CLIENTEDGE)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
(36,26) : WARNING: $WM_NOTIFY: possibly used before declaration.
GUIRegisterMsg($WM_NOTIFY,
~~~~~~~~~~~~~~~~~~~~~~~~~^
(80,58) : ERROR: _ArraySort() called with wrong number of args.
_ArraySort($aItemsTemp, 0, 1, 0, $iDimension, $cIndex)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
(884,89) : REF: definition of _ArraySort().
Func _ArraySort(ByRef $avArray, $iDescending = 0, $iStart = 0, $iEnd = 0, $iSubItem = 0)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
(81,113) : ERROR: _ArraySort() called with wrong number of args.
If StringInStr($iCurPos, $aItemsTemp[1][$cIndex]) Then _ArraySort($aItemsTemp, 1, 1, 0, $iDimension, $cIndex)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
(884,89) : REF: definition of _ArraySort().
Func _ArraySort(ByRef $avArray, $iDescending = 0, $iStart = 0, $iEnd = 0, $iSubItem = 0)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
(8,145) : ERROR: $WS_EX_CLIENTEDGE: undeclared global variable.
$hListView = _GUICtrlListView_Create($GUI, "Items|SubItems1|SubItems2", 20, 15, 260, 170, BitOR($LVS_EDITLABELS, $LVS_REPORT), $WS_EX_CLIENTEDGE)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
- 3 error(s), 2 warning(s)
Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites

goldenix

Hi! It appears that in AutoIt v.3.2.12.0 _ArraySort() function is changed. In previous AutoIt version was defined a

$i_Dim

Optional: Number of occurences in the second dimension

eg $A[100] = 0, $A[100][2] = 2, $A[100][5] = 5

parameter, but in new version this param is removed. Now i change example, first post updated. :)

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

goldenix

Hi! It appears that in AutoIt v.3.2.12.0 _ArraySort() function is changed. In previous AutoIt version was defined a

parameter, but in new version this param is removed. Now i change example, first post updated. :)

Great this is what I was looking for. Thanx.

EDIT: 1 thing: If user has too many Items in the list your code returns Array limit exeeded Error.

I Forgot, Can you put back the old code for Autoit, I forgot to make a copy of it just in case Ill need to use older autoit version.

_ListView_Sort(2) ; Sort Column 2

;===============================================================================
;
; Function Name:    _ListView_Sort()
; Description:      Sorting ListView items when column click
; Parameter(s):     $cIndex - Column index
; Return Value(s):  None
; Requirement(s):   AutoIt 3.2.12.0 and above
; Author(s):        R.Gilman (a.k.a rasim)
;
;================================================================================
Func _ListView_Sort($cIndex)
    
    $cIndex = $cIndex -1
    
    Local $iColumnsCount, $iDimension, $iItemsCount, $aItemsTemp, $aItemsText, $iCurPos, $iImgSummand, $i, $j
   
    $iColumnsCount = _GUICtrlListView_GetColumnCount($hListView)
   
    $iDimension = $iColumnsCount * 2
   
    $iItemsCount = _GUICtrlListView_GetItemCount($hListView)
   
    Local $aItemsTemp[1][$iDimension]
   
    For $i = 0 To $iItemsCount - 1
        $aItemsTemp[0][0] += 1
        ReDim $aItemsTemp[$aItemsTemp[0][0] + 1][$iDimension]
       
        $aItemsText = _GUICtrlListView_GetItemTextArray($hListView, $i)
        $iImgSummand = $aItemsText[0] - 1
       
        For $j = 1 To $aItemsText[0]
            $aItemsTemp[$aItemsTemp[0][0]][$j - 1] = $aItemsText[$j]
            $aItemsTemp[$aItemsTemp[0][0]][$j + $iImgSummand] = _GUICtrlListView_GetItemImage($hListView, $i, $j - 1)
        Next
    Next
   
    $iCurPos = $aItemsTemp[1][$cIndex]
    _ArraySort($aItemsTemp, 0, 1, 0, $cIndex)
    If StringInStr($iCurPos, $aItemsTemp[1][$cIndex]) Then _ArraySort($aItemsTemp, 1, 1, 0, $cIndex)
   
    For $i = 1 To $aItemsTemp[0][0]
        For $j = 1 To $iColumnsCount
            _GUICtrlListView_SetItemText($hListView, $i - 1, $aItemsTemp[$i][$j - 1], $j - 1)
            _GUICtrlListView_SetItemImage($hListView, $i - 1, $aItemsTemp[$i][$j + $iImgSummand], $j - 1)
        Next
    Next
EndFunc
;================================================================================
Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites

goldenix

If user has too many Items in the list your code returns Array limit exeeded Error

I tested this example with ListView contains a 500 items & subitems, works fine, but slowly, because example used a _ArraySort() function.

I Forgot, Can you put back the old code for Autoit, I forgot to make a copy of it just in case Ill need to use older autoit version.

As i say in new AutoIt version you can use a _GUICtrlListView_SortItems(), because this function works very fast and rightly. Just add a Number of occurences in the second dimension parameter in _ArraySort() function.

Share this post


Link to post
Share on other sites

Thank you!!! just what I was looking for!

Share this post


Link to post
Share on other sites

goldenix

I tested this example with ListView contains a 500 items & subitems, works fine, but slowly, because example used a _ArraySort() function.

As i say in new AutoIt version you can use a _GUICtrlListView_SortItems(), because this function works very fast and rightly. Just add a Number of occurences in the second dimension parameter in _ArraySort() function.

_GUICtrlListView_SortItems() is not functional, I have tried to make it sort my column for days & all I was able to do is to make it sort my column by clicking on the column, But this is not what I need. What I need is to make it sort my column by calling a function _GUICtrlListView_Sortcolumn($col_nr). And so far your UDF was the only one I was able to do that with.


My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites

goldenix

What I need is to make it sort my column

Mayb this?

Share this post


Link to post
Share on other sites

just

Dim $B_DESCENDING[_GUICtrlListView_GetColumnCount ($ListView3_1_1)]

GUICtrlSetOnEvent($ListView3_1_1,"compositor")

Func compositor()

GUICtrlListView_SimpleSort ($ListView3_1_1, $B_DESCENDING, GUICtrlGetState($ListView3_1_1))

EndFunc

is ok

Share this post


Link to post
Share on other sites

ken0137

Ok, but if items contains images? ;)

Share this post


Link to post
Share on other sites

hi guy i have i little problem whit this

_GUICtrlListView_RegisterSortCallBack($hListView)

While 1

Switch GUIGetMsg()

Case $GUI_EVENT_CLOSE

ExitLoop

Case $hListView

; Kick off the sort callback

_GUICtrlListView_SortItems($hListView, GUICtrlGetState($hListView))

EndSwitch

WEnd

_GUICtrlListView_UnRegisterSortCallBack($hListView)

so i use a different logic for do a program

i put this in dow at page

;===============================================================

;Keep the GUI alive

;===============================================================

While 1

Sleep(1000)

WEnd

and use a series of funcrion

and whit button call a function

in one of this function i i have a listview and want order the item when i push at top of coulum i try to insert at down this code:

_GUICtrlListView_RegisterSortCallBack($ListView1)

While 1

Switch GUIGetMsg()

Case $GUI_EVENT_CLOSE

ExitLoop

Case $ListView1

; Kick off the sort callback

_GUICtrlListView_SortItems($ListView1, GUICtrlGetState($ListView1))

EndSwitch

WEnd

_GUICtrlListView_UnRegisterSortCallBack($ListView1)

but dont do nothing and stay in loop and gui dont exit , why?? how can tell when i push a top of listview do a sort item?? thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0