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.


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)

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

    ; 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
            $iLV_Width += $iData_Width

    ; 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
            ; Prevent resizing
            Return True
            ; Sort column
            $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
            $iCol = DllStructGetData($tInfo, "SubItem")
            _GUICtrlListView_SimpleSort($cListView, $g_bSortSense, $iCol)

EndFunc   ;==>_WM_NOTIFY
Edited by poila

Share this post

Link to post
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

Share this post

Link to post
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

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  

  • Similar Content

    • nooneclose
      By nooneclose
      How to use _Excel_RangeSort to sort my excel file by three different headers Column A1, B1, and C1 have headers on which I want to sort by. The headers on which I want to sort are department, employee type, and name.
      I still really new to AutoIt so I do not actually know how to properly start this line or lines of code, to be honest. The example code is the best I can do.
      _Excel_RangeSort($OpenWorkbook, Default, "A1:C1", "1:1", $xlDescending, Default, $xlYes, Default, $xlSortRows) I just need to sort by those three headers in that order of department, employee type, and name, plus in descending order.
      any and all help would be greatly appreciated.  Thank you!
    • Haselnuzz
      By Haselnuzz
      Hi and Hello from a Noob..:-)
      i have a very weird problem. I fill up a 2d array with a)numbers and b)letters from A-Z, so 2 columns. This works absolutely perfect. But as soon as i try to sort them (numbers ascending) the array ends up in some kind of "String-sortation". What exactly am i doin wrong? To make it easy, i post below the piece of code, which i am talking about. Hope that someone can help me out.
      Func Analyse()
          Local $BasisArray[0]
          Local $aFill = "A" & "|B" & "|C" & "|D" & "|E" & "|F" & "|G" & "|H" & "|I" & "|J" & "|K" & "|L" & "|M" & "|N" & "|O" & "|P" & "|Q" & "|R" & "|S" & "|T" & "|U" & "|V" & "|W" & "|X" & "|Y" & "|Z"
          _ArrayAdd ($BasisArray, $afill)
          Local $FreqArray[0][2]=[[]]
          $row = 0

          For $i = 0 to 25
              $fummel = _ArrayToString ($BasisArray, ":" , $row, $row)
              $readout = _GUICtrlRichEdit_GetText ($hRichEdit)
              $anzAs = stringreplace ($readout, $fummel, $fummel)
              $extended = @extended
              $FreqFill = $extended & "|" & $fummel
              _ArrayAdd($FreqArray, $FreqFill)
              $row = $row + 1
          _ArrayDisplay($FreqArray, "2D - Item delimited")
          _ArrayDisplay($FreqArray, "bla")
      Thanks for helping me,
    • liagason
      By liagason
      Hello everyone,
      How can I display in ascending  sequence some numbers stored in a string variable?
      $str = "18,03,48,23" MsgBox(0,"test",$str) I would like it to display "03,18,23,48"
    • TrashBoat
      By TrashBoat
      So I've made this script that detects how long i have held down my left mouse button for and stores the information in an array and then sorts its using _ArraySort but the output is half sorted half broken.
      Here's my script:
      HotKeySet("{F1}","_exit") #include <Misc.au3> #include <Timers.au3> #include <Array.au3> Local $dll = DllOpen("user32.dll") $on = False Global $array[0] While(1) If _IsPressed(01,$dll) Then $timer = _Timer_Init() While _IsPressed(01,$dll) Sleep(1) WEnd $time = _Timer_Diff($timer) _ArrayAdd($array,"Time: " & Floor($time) & " ms") ;~ ConsoleWrite("Time: " & Floor($time) & " ms" & @CRLF) EndIf Sleep(50) WEnd Func _exit() _ArraySort($array) _ArrayDisplay($array) Exit EndFunc And the output:

      See how its not sorted?  What is the problem here?
    • AndreasNWWWWW
      By AndreasNWWWWW
      Hi, i'm just curious is there a way to sort an ini file after largest first?
      my inifiles writes itself like this
      but is there a way so i can get it sortet