Jump to content

Listview sort column 0 (numbers) column 1 (strings)


Go to solution Solved by LarsJ,

Recommended Posts

Hi,

just a short question. 
I 've got a lisitview with a few columns and I want to register a different sort for two kinds of columns.
I 've got a solution, but I don't think it is a good one. 

Example : 
Col A = Names (Strings)
Col B = Age (Integers)

Right now I'm doing this:
1. _GUICtrlListView_RegisterSortCallBack
2. In my WM_NOTIFY 
Case GUICtrlGetHandle($search_LV)
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $iLparam)
                    ; Kick off the sort callback
;~                  ConsoleWrite('SEARCH_LV Column geklickt' & @LF)
                    ConsoleWrite("Sort: " & DllStructGetData($tInfo, 'SubItem'))
                    If $ColumnSorted <> DllStructGetData($tInfo, 'SubItem') Then
                        _GUICtrlListView_UnRegisterSortCallBack($search_LV)
                        ConsoleWrite('NEU' & @CRLF)
                    EndIf
                    $ColumnSorted = DllStructGetData($tInfo, 'SubItem')
                    If DllStructGetData($tInfo, 'SubItem') = 9 Or DllStructGetData($tInfo, 'SubItem') = 10 Then
                        ConsoleWrite('True: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, True) & @CRLF)
                    Else
                        ConsoleWrite('False ' & _GUICtrlListView_RegisterSortCallBack($search_LV, False) & @CRLF)
                    EndIf
                    _GUICtrlListView_SortItems($search_LV, DllStructGetData($tInfo, 'SubItem'))

So, I store the column clicked in a global variable. If the column is clicked again (sort asc or desc) then all is fine, if another column then the one before is clicked, I unregister the callback and create a new one.

The new callback (in the if) depends on the column clicked because of the content to sort (strings or numbers).

Is there an easier / better way?

Do I have to use my own sorting function with GuiCtrlRegisterListViewSort?

Edit: It still doesn't work in every case (just noticed).

Thanks

Mega

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Okay, maybe it is a little bit different to understand what I want.

Here is an example script.

Whats needs to be changed to get all comlums sortable correctly (literal,numeric and asc,desc)?

#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <StructureConstants.au3>

Opt('MustDeclareVars', 1)

Global $search_LV, $B_DESCENDING
GUICreate("ListView Sort by Column Click", 400, 300)
$search_LV = GUICtrlCreateListView("String|Number|String", 2, 2, 394, 268)
GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlCreateListViewItem("line4|5|1A", $search_LV)
GUICtrlCreateListViewItem("line5|4.50 |1B", $search_LV)
GUICtrlCreateListViewItem("line5|4.0 |2C", $search_LV)
GUICtrlCreateListViewItem("line3|23|01", $search_LV)
GUICtrlCreateListViewItem("line2|0.34560 |09", $search_LV)
GUICtrlCreateListViewItem("line1|1.0 |7A", $search_LV)
GUICtrlCreateListViewItem("line1|0.1 |8C", $search_LV)
GUICtrlCreateListViewItem("line1|97|5B", $search_LV)
GUICtrlCreateListViewItem("line1|910|9B", $search_LV)
GUICtrlCreateListViewItem("line1|99|11", $search_LV)
GUICtrlCreateListViewItem("line1|990.99|06", $search_LV)
_GUICtrlListView_SetColumnWidth($search_LV, 0, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 1, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 2, 75)
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
;~ _GUICtrlListView_RegisterSortCallBack($search_LV, False)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $search_LV
            ; Kick off the sort callback
;~          _GUICtrlListView_SortItems($search_LV, GUICtrlGetState($search_LV))
;~          _GUICtrlListView_UnRegisterSortCallBack($search_LV)
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $search_LV
;~  If Not IsHWnd($search_LV) Then $hWndListView = GUICtrlGetHandle($search_LV)

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

    Switch $hWndFrom
;~      Case $search_LV
        Case GUICtrlGetHandle($search_LV)
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    ConsoleWrite("Header geklickt" & @CRLF)
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $iLparam)
                    _GUICtrlListView_UnRegisterSortCallBack($search_LV)
                    Local $ColumnSorted = DllStructGetData($tInfo, 'SubItem')
                    If $ColumnSorted = 1 Then
                        ConsoleWrite('Numeric: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, True) & @CRLF)
                    Else
                        ConsoleWrite('Literal: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, False) & @CRLF)
                    EndIf
                    _GUICtrlListView_SortItems($search_LV, DllStructGetData($tInfo, 'SubItem'))
            EndSwitch
    EndSwitch
EndFunc   ;==>WM_NOTIFY

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

I don't what the problem is with this question, but maybe many people think: "oh, it is an MVP who asked, then it has to be a very difficult question."

Anyhow, I found a solution but wasn't able to figure out how to do it with the RegisterCallBack funtion.

Here ist what I have so far (Still missing are the little arrows in the column  header, but I think I can handle that later)

#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <StructureConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>

Opt('MustDeclareVars', 1)

Global $g_iCurCol = -1
Global $g_iSortDir = 1
Global $g_bSet = False
Global $g_iCol = -1

Global $search_LV
GUICreate("ListView Sort by Column Click", 400, 300)
$search_LV = GUICtrlCreateListView("String|Number|String", 2, 2, 394, 268)
GUICtrlRegisterListViewSort(-1, "LVSort") ; Register the function "SortLV" for the sorting callback
GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlCreateListViewItem("line4|5|1A", $search_LV)
GUICtrlCreateListViewItem("line5|4.50 |1B", $search_LV)
GUICtrlCreateListViewItem("line5|4.0 |2C", $search_LV)
GUICtrlCreateListViewItem("line3|23|01", $search_LV)
GUICtrlCreateListViewItem("line2|0.34560 |09", $search_LV)
GUICtrlCreateListViewItem("line1|1.0 |7A", $search_LV)
GUICtrlCreateListViewItem("line1|0.1 |8C", $search_LV)
GUICtrlCreateListViewItem("line1|97|5B", $search_LV)
GUICtrlCreateListViewItem("line1|910|9B", $search_LV)
GUICtrlCreateListViewItem("line1|99|11", $search_LV)
GUICtrlCreateListViewItem("line1|990.99|06", $search_LV)
_GUICtrlListView_SetColumnWidth($search_LV, 0, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 1, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 2, 75)
GUISetState()

Global $ColumnOLD, $ColumnNew

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $search_LV
            ; Kick off the sort callback
            $g_bSet = False
            $g_iCurCol = $g_iCol
            GUICtrlSendMsg($search_LV, '', GUICtrlGetState($search_LV), 0)
    EndSwitch
WEnd

; Our sorting callback funtion
Func LVSort($hWnd, $nItem1, $nItem2, $iColumn)
    Local $sVal1, $sVal2, $iResult

    ; Switch the sorting direction
    If $iColumn = $g_iCurCol Then
        If Not $g_bSet Then
            $g_iSortDir = $g_iSortDir * -1
            $g_bSet = True
        EndIf
    Else
        $g_iSortDir = 1
    EndIf
    $g_iCol = $iColumn

    $sVal1 = GetSubItemText($hWnd, $nItem1, $iColumn)
    $sVal2 = GetSubItemText($hWnd, $nItem2, $iColumn)

    If $iColumn = 1 Then
        $sVal1 = Number($sVal1)
        $sVal2 = Number($sVal2)
    EndIf

    $iResult = 0 ; No change of item1 and item2 positions

    If $sVal1 < $sVal2 Then
        $iResult = -1 ; Put item2 before item1
    ElseIf $sVal1 > $sVal2 Then
        $iResult = 1 ; Put item2 behind item1
    EndIf

    $iResult = $iResult * $g_iSortDir

    Return $iResult
EndFunc   ;==>LVSort

; Retrieve the text of a listview item in a specified column
Func GetSubItemText($idCtrl, $idItem, $iColumn)
    Local $tLvfi = DllStructCreate("uint;ptr;int;int[2];int")

    DllStructSetData($tLvfi, 1, $LVFI_PARAM)
    DllStructSetData($tLvfi, 3, $idItem)

    Local $tBuffer = DllStructCreate("char[260]")

    Local $nIndex = GUICtrlSendMsg($idCtrl, $LVM_FINDITEM, -1, DllStructGetPtr($tLvfi));

    Local $tLvi = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int")

    DllStructSetData($tLvi, 1, $LVIF_TEXT)
    DllStructSetData($tLvi, 2, $nIndex)
    DllStructSetData($tLvi, 3, $iColumn)
    DllStructSetData($tLvi, 6, DllStructGetPtr($tBuffer))
    DllStructSetData($tLvi, 7, 260)

    GUICtrlSendMsg($idCtrl, $LVM_GETITEMA, 0, DllStructGetPtr($tLvi));

    Local $sItemText = DllStructGetData($tBuffer, 1)

    Return $sItemText
EndFunc   ;==>GetSubItemText

If somebody has an exmaple how to sort two columns differently with UDF_Callback_functions or at least shows the concept how to do, I'm still interested. THANKS!

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • Solution

You should not unregister/register the sort function after every sort. Only when you sort by another column.

This is the code in post 2:

#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <StructureConstants.au3>

Opt('MustDeclareVars', 1)

Global $search_LV, $B_DESCENDING
GUICreate("ListView Sort by Column Click", 400, 300)
$search_LV = GUICtrlCreateListView("String|Number|String", 2, 2, 394, 268)
GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlCreateListViewItem("line4|5|1A", $search_LV)
GUICtrlCreateListViewItem("line5|4.50 |1B", $search_LV)
GUICtrlCreateListViewItem("line5|4.0 |2C", $search_LV)
GUICtrlCreateListViewItem("line3|23|01", $search_LV)
GUICtrlCreateListViewItem("line2|0.34560 |09", $search_LV)
GUICtrlCreateListViewItem("line1|1.0 |7A", $search_LV)
GUICtrlCreateListViewItem("line1|0.1 |8C", $search_LV)
GUICtrlCreateListViewItem("line1|97|5B", $search_LV)
GUICtrlCreateListViewItem("line1|910|9B", $search_LV)
GUICtrlCreateListViewItem("line1|99|11", $search_LV)
GUICtrlCreateListViewItem("line1|990.99|06", $search_LV)
_GUICtrlListView_SetColumnWidth($search_LV, 0, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 1, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 2, 75)
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
;~ _GUICtrlListView_RegisterSortCallBack($search_LV, False)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $search_LV
            ; Kick off the sort callback
;~          _GUICtrlListView_SortItems($search_LV, GUICtrlGetState($search_LV))
;~          _GUICtrlListView_UnRegisterSortCallBack($search_LV)
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local Static $ColumnSortedPrev = -1
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $search_LV
;~  If Not IsHWnd($search_LV) Then $hWndListView = GUICtrlGetHandle($search_LV)

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

    Switch $hWndFrom
;~      Case $search_LV
        Case GUICtrlGetHandle($search_LV)
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    ConsoleWrite("Header geklickt" & @CRLF)
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $iLparam)
                    Local $ColumnSorted = DllStructGetData($tInfo, 'SubItem')
                    If $ColumnSortedPrev <> $ColumnSorted Then
                      If $ColumnSortedPrev > -1 Then _GUICtrlListView_UnRegisterSortCallBack($search_LV)
                      If $ColumnSorted = 1 Then
                          ConsoleWrite('Numeric: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, True) & @CRLF)
                      Else
                          ConsoleWrite('Literal: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, False) & @CRLF)
                      EndIf
                      $ColumnSortedPrev = $ColumnSorted
                    EndIf
                    _GUICtrlListView_SortItems($search_LV, DllStructGetData($tInfo, 'SubItem'))
            EndSwitch
    EndSwitch
EndFunc   ;==>WM_NOTIFY
Link to comment
Share on other sites

Thanks!  :thumbsup: 

Now I've got two working solutions.

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
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
 Share

×
×
  • Create New...