Jump to content

Recommended Posts

I was reading the Helpfile on both GUICtrlRegisterListViewSort and _GUICtrlListView_SimpleSort, and after running my script, I was initially satisfied with the way columns are sorted when the user clicks on the column header.

But at certain occasions, clicking on the column header to sort numbers - integers in particular - seem to have some unexpected behaviour.

I cannot verify if it is due to the way I wrote the script, or because the functions used in AutoIt to sort numbers intentionally work the way they do.

Example:

Column ID | Item ID | Item Name | Date

1 | ID_1 | One | 14/05/2014

2 | ID_2 | Two| 14/05/2014

3 | ID_3 | Three| 14/05/2014

10 | ID_4 | Four| 14/05/2014

9 | ID_5 | Five| 14/05/2014

27 | ID_6 | Siz| 15/05/2014

When the user clicks Column ID, logically the order of ascending sorting would look like this:

1, 2, 3, 9, 10, 27

But what I got is like this:

1, 10, 2, 27, 3, 9

Does anyone know the reason behind the way it is sorted this way?

How then do I get the columns to be sorted correctly when the user clicks the column header?

(I did read across other related threads like  and )

I attached my codes here to see if I might write them wrongly.

My code for generating ListView:

$inventoryList = GUICtrlCreateListView("ID|item id|item name| Date", 45, 140, expandGUIX(750), expandGUIY(450), $LVS_REPORT, BitOR($LVS_EX_FULLROWSELECT, $WS_EX_CLIENTEDGE))
_GUICtrlListView_SetColumnWidth(-1, 3, 651)
$hInventoryList = GUICtrlGetHandle($inventoryList)
For $i = 0 To 3
    _GUICtrlListView_SetColumnWidth($inventoryList, $i, $LVSCW_AUTOSIZE_USEHEADER)
Next

My code for auto-resizing columns:

Func resizeColumns($hWnd, $listView, $iColumns, $iGUI_Width, $iGUI_Height)
    ; Check top index - if not 0 then we have a scroll bar so increase ListView width
    Local $iScroll_Allowance
    If _GUICtrlListView_GetTopIndex($listView) > 0 Then
        $iScroll_Allowance = 17
    EndIf

    ; Determine ListView width
    Local $iLV_Width = $iScroll_Allowance
    Local $iData_Width, $iHeader_Width
    Local $i = 0
    For $i = 0 To $iColumns - 1
        ; Size column to fit header
        _GUICtrlListView_SetColumnWidth($listView, $i, $LVSCW_AUTOSIZE_USEHEADER)
        $iHeader_Width = _GUICtrlListView_GetColumnWidth($listView, $i)
        ; Now size column to fit data
        _GUICtrlListView_SetColumnWidth($listView, $i, $LVSCW_AUTOSIZE)
        $iData_Width = _GUICtrlListView_GetColumnWidth($listView, $i)
        ; If header is wider, reset width
        If $iHeader_Width > $iData_Width Then
            _GUICtrlListView_SetColumnWidth($listView, $i, $iHeader_Width)
            $iLV_Width += $iHeader_Width
        Else
            $iLV_Width += $iData_Width
        EndIf
    Next

    ; Resize ListView and GUI to fit data
    ControlMove($hWnd, "", $listView, 10, 10, $iLV_Width + 10, $iGUI_Height - 20) ; Add 10 for internal ListView borders
EndFunc   ;==>resizeColumns

My code for locking users from resizing columns after auto-resize:

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

    ; Get details of message
    Local $tNMHEADER = DllStructCreate($tagNMHEADER, $lParam)
    ; Look for header resize code
    $iCode = DllStructGetData($tNMHEADER, "Code")
    Switch $iCode
        Case $HDN_BEGINTRACKW
            ; Prevent resizing
            Return True
        Case $LVN_COLUMNCLICK
            ; Sort column
            $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
            $iCol = DllStructGetData($tInfo, "SubItem")
            _GUICtrlListView_SimpleSort($cListView, $g_bSortSense, $iCol)
    EndSwitch

    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY
Edited by poila
Link to comment
Share on other sites

It's called a lexical or lexicographical sort, it sorts by the character values, not the numeric values.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hi BrewManNH,

Thanks for the tip. I just did a search on the forums and came across this thread that's related to lexical sort ('?do=embed' frameborder='0' data-embedContent>>).

(Gonna add 'lexical' to my vocab, sounds new to me ._. )

Not sure of how to integrate the UDF (shown inside the link) into a ListView sort, since the link I shared is for sorting arrays instead of ListView.

Any ideas or has anyone else did proper number sorting before for ListView?

Edited by poila
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...