Jump to content
Sign in to follow this  

Sorting a listview just descending

Recommended Posts


I've been looking at the examples for _GuiCtrlListView_SimpleSort and _GUICtrlListView_RegisterSortCallBack..._GUICtrlListView_SortItems... _GUICtrlListView_UnRegisterSortCallBack in the Help File, and I can't figure out how to modify either to do just a descending sort...

The examples basically give behavior where you click a column once and it sorts it ascending, click it again and it sorts it descending, then ascending, and so on.

I'm trying to figure out how to make it so each time you click a column it reorders the listview again based on that column in descending order. Any thoughts on how or if it's possible?

Share this post

Link to post
Share on other sites

Try this, modified from Autoit help file

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work

Global $hListView, $B_DESCENDING

Func _Main()
    GUICreate("ListView SimpleSort", 400, 300)
    $hListView = GUICtrlCreateListView("col1|col2|col3", 2, 2, 394, 268)
    GUICtrlCreateListViewItem("line4|5|a", $hListView)
    GUICtrlCreateListViewItem("line5|4|b", $hListView)
    GUICtrlCreateListViewItem("line6|3|c", $hListView)
    GUICtrlCreateListViewItem("line7|2|d", $hListView)
    GUICtrlCreateListViewItem("line8|1|e", $hListView)
    _GUICtrlListView_SetColumnWidth($hListView, 0, 75)
    _GUICtrlListView_SetColumnWidth($hListView, 1, 75)
    _GUICtrlListView_SetColumnWidth($hListView, 2, 75)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    ; Loop until user exits
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

EndFunc   ;==>_Main

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $tInfo, $iCol
    $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 ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    $iCol = _GUICtrlListView_GetColumnCount($hListView)
                    Dim $B_DESCENDING[$iCol]
                    For $i = 0 To $iCol - 1
                        $B_DESCENDING[$i] = True ; false = ascending, true = descending
                    _GUICtrlListView_SimpleSort($hWndListView, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
EndFunc   ;==>WM_NOTIFY

Edited by picaxe

Share this post

Link to post
Share on other sites

That's exactly what I was looking for!

I know it basically says to do that in the syntax explanation, but it's not the most intuitive thing to know how to add to a script...

Thank you sir!

Share this post

Link to post
Share on other sites

Took me a little while to work it out as well muttley

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