Sign in to follow this  
Followers 0

Does ListView sorting sort integers correctly?

3 posts in this topic

#1 ·  Posted (edited)

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 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

#3 ·  Posted (edited)

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  
Followers 0

  • Similar Content

    • Clemens
      By Clemens
      Hi there,
      I have a small hopefully quick fixable issue with reading information from my ListView:
      So in fact I just want to have information about which items are selected, so I'm using
      msgbox(0 , "return", GUICtrlRead($myListView), 1)
      but unfortunately it only returns me either the first item id or if this is not selected the second item id, or if this is not selected the third, etc. or 0 if none is selected.
      Anybody has an idea how to get the full picture of my >>multiple Items selected<< ListView?
      Thanks in advance!
    • FrancescoDiMuro
      By FrancescoDiMuro
      Hi guys! How are you? Hope you're fine
      I'm trying to use SQLite for managing some data, and, I would like to display my "retrieved" data, but I'm trying to do a select from 2 table, and seems to not work properly...
      What I'd like to do is retrieve data from 2 tables and display in a listview...
      I tried with:
      Local $aRisultato, $iRighe, $iColonne, $iRVal $iRval = _SQLite_GetTable2d($hDatabase, "SELECT * FROM Magazzino_Rockwell, Magazzino_Siemens;", $aRisultato, $iRighe, $iColonne) If $iRVal = $SQLITE_OK Then ;_SQLite_Display2DResult($aRisultato) _ArrayDisplay($aRisultato) EndIf ... but it displays a single record 41 times, and it does this thing for every record in the database...
      What do you need to help me?

      Managed with a double If and double query like this:
      $iRval = _SQLite_GetTable2d($hDatabase, "SELECT * FROM Magazzino_Rockwell;", $aRisultato, $iRighe, $iColonne) If $iRVal = $SQLITE_OK Then Local $aRisultatoRockwell = $aRisultato $iRval = _SQLite_GetTable2d($hDatabase, "SELECT * FROM Magazzino_Siemens;", $aRisultato, $iRighe, $iColonne) If $iRVal = $SQLITE_OK Then _ArrayConcatenate($aRisultato, $aRisultatoRockwell) Local $i, $sRiga, $s_LV_Item For $i = 1 To Ubound($aRisultato) - 1 $sRiga = $aRisultato[$i][0] & "|" & _ $aRisultato[$i][1] & "|" & _ $aRisultato[$i][2] & "|" & _ $aRisultato[$i][3] & "|" & _ $aRisultato[$i][4] & "|" & _ $aRisultato[$i][5] & "|" & _ $aRisultato[$i][6] & "|" & _ $aRisultato[$i][7] & "|" & _ $aRisultato[$i][8] & "|" & _ $aRisultato[$i][9] & "|" & _ $aRisultato[$i][10] & "|" & _ $aRisultato[$i][11] $s_LV_Item = GUICtrlCreateListViewItem($sRiga, $lv_Lista) Next And so, the listview is created
      If anyone has another more efficient way, I'm here Thanks guys  
    • rootx
      By rootx
      How can I change dynamically the background colors of the selected Item? THX
      I try with...
      #include <ListBoxConstants.au3> #include <GuiListBox.au3> #include <GuiListView.au3> #include <MsgBoxConstants.au3> #include <GUIConstantsEx.au3> #include <ListViewConstants.au3> #include <WindowsConstants.au3> Global $hListView #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 615, 437, 192, 124) GUICreate("listview items", 220, 250, 100, 200, -1, $WS_EX_ACCEPTFILES) GUISetBkColor(0x00E0FFFF) ; will change background color $idListview = GUICtrlCreateListView("col1 |col2|col3 ", 10, 10, 200, 150) ;,$LVS_SORTDESCENDING) _GUICtrlListView_SetBkColor($idListview, 0xff0000) _GUICtrlListView_SetTextBkColor($idListview, 0xff0000) _GUICtrlListView_SetTextColor($idListview, 0x0000FF00) $idButton = GUICtrlCreateButton("Value?", 75, 170, 70, 20) $idItem1 = GUICtrlCreateListViewItem("item1|col22|col23", $idListview) $idItem2 = GUICtrlCreateListViewItem("item2|col12|col13", $idListview) $idItem3 = GUICtrlCreateListViewItem("item3|col32|col33", $idListview) $idItem4 = GUICtrlCreateListViewItem("item4|col32|col33", $idListview) $idItem5 = GUICtrlCreateListViewItem("item5|col32|col33", $idListview) $idItem9 = GUICtrlCreateListViewItem("item6|col32|col33", $idListview) GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) Local $hWndFrom, $iCode, $tNMHDR, $hWndListView $hWndListView = $idListview If Not IsHWnd($idListview) Then $hWndListView = GUICtrlGetHandle($idListview) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndListView Switch $iCode Case $NM_CLICK ; The user has clicked the left mouse button within the control Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam) $Index = DllStructGetData($tInfo, "Index") $ctrlID = _GUICtrlListView_GetItemParam($idListview,$Index) Local $iItemText = _GUICtrlListView_GetItemText($idListview, DllStructGetData($tInfo, "Index"), 0) ConsoleWrite($iItemText&@CRLF) _GUICtrlListView_SetTextBkColor($idListview, 0x2E8B57) ;If BitAND(GUICtrlRead($idListview), $GUI_CHECKED) Then ; GUICtrlSetBkColor($ctrlID, 0xff0000) ;Else ; GUICtrlSetBkColor($ctrlID, 0xffff00) ;EndIf EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIF  
    • Wingens
      By Wingens
      I am trying to create a list view wich shows the content of an ini file. There is a pulldown menu wich allows me to check a certain status in the ini file if the status is found it should read out the key and in every section the same key is located after that it should fill out the listview with all results found.
      $OVERZICHTGUI = GUICreate("RMA Overzicht", 1235, 742, -1, -1) GUICtrlCreateLabel("Ingelogd als: " & $NAAMMEDEWERKER, 8, 0) GUICtrlSetState(-1, $GUI_DISABLE) GUICtrlCreateGroup("", 8, 16, 185, 89) GUICtrlCreateLabel("Status:", 32, 40, 37, 17) $STATUS = GUICtrlCreateCombo("", 32, 64, 140, 25,BitOR($CBS_DROPDOWNLIST, $WS_VSCROLL)) GUICtrlSetData($STATUS, "Aangevraagd|Verzonden|Afgehandeld", "") GUICtrlCreateGroup("", -99, -99, 1, 1) $View = GUICtrlCreateListView("RMANR|RMANRLEV|DEBITNR|KLANT|STATUS|MODEL|SERIENR|AANGEMELDDOOR|AANMELDDATUM|VERZENDDATUM|RETOURDATUM|KLACHT|OPLOSSING", 8, 120, 1218, 614) $Exit = GUICtrlCreateButton("Hoofdmenu", 1104, 8, 123, 33) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $EXIT $YesOrNo = msgBox(4,"","Terug naar het hoofdmenu?") If $YesOrNo = 6 then GUIDelete($OVERZICHTGUI) MAINMENU() ExitLoop EndIf Case $STATUS $GO = 1 $STATE = GUICtrlRead($STATUS) Local $aArray = IniReadSection(@ScriptDir & "\DB\rma.ini", "STATUS") _ArrayDisplay($aArray, "Gevonden") $FOUNDRMANR = _ArraySearch($aArray, $STATE) MsgBox(4096, "", $FOUNDRMANR) If $FOUNDRMANR = -1 Then MsgBox(4096, "Not Found", "No RMA requests with this status found.") $GO = 0 EndIf If $GO = 1 Then $VAR = IniRead(@ScriptDir & "\DB\rma.ini", "RMANR", $FOUNDRMANR, "Not found") MsgBox(4096, "", $VAR) EndIf EndSwitch WEnd  
    • Xibalba
      By Xibalba
      Hello again dear forum!
      I want to switch "only text" with "image plus text" in each cell, but can't figure it out.
      #include <GuiConstantsEx.au3> #include <ColorConstants.au3> #include <GuiImageList.au3> #include <GuiListView.au3> #include <MsgBoxConstants.au3> Opt("GUIOnEventMode", 1) ; GUI Local $StatusBox_Width = 800 Local $StatusBox_Height = 400 Local $hMainGUI = GUICreate("Example", $StatusBox_Width, $StatusBox_Height) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton") ; GUI MESSAGE LOOP GUISetState(@SW_SHOW, $hMainGUI) ; LIST VIEW Local $iListView = GUICtrlCreateListView("", 15, 40, 530, 80) _GUICtrlListView_SetExtendedListViewStyle($iListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)) ; Load images Local $hImage = _GUIImageList_Create() _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($iListView), 0xFF0000, 16, 16)) _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($iListView), 0x00FF00, 16, 16)) _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($iListView), 0x0000FF, 16, 16)) _GUICtrlListView_SetImageList($iListView, $hImage, 1) ; Add columns _GUICtrlListView_AddColumn($iListView, "Column 1", 80) _GUICtrlListView_AddColumn($iListView, "Column 2", 80) _GUICtrlListView_AddColumn($iListView, "Column 3", 80) _GUICtrlListView_AddColumn($iListView, "Column 4", 80) _GUICtrlListView_AddColumn($iListView, "Column 5", 80) ; Add items _GUICtrlListView_AddItem($iListView, "One") _GUICtrlListView_AddSubItem($iListView, 0, "", 1, 1) _GUICtrlListView_AddSubItem($iListView, 0, "", 2, 2) GUICtrlCreateListViewItem("1|2|3|4|5", $iListView) MsgBox($MB_SYSTEMMODAL, "Test", "Updating cells...") ; Update cells _GUICtrlListView_SetItem($iListView, "New text", 0, 1) ; OK _GUICtrlListView_SetItem($iListView, "One(1)", 0, 0, 88) ; Here I want to remove the image completely (only way to HIDE image is to put an index that doesn't exist? - 88 in this case) _GUICtrlListView_AddItem($iListView, "Three", 7) ; Here I want to create the text without any image (space) - how? _GUICtrlListView_SetItemImage($iListView, 1, 0, 1) ; OK _GUICtrlListView_SetItemImage($iListView, 0, 1, 2) ; OK While 1 Sleep(100) ; Sleep to reduce CPU usage WEnd Func CLOSEButton() ; Note: At this point @GUI_CtrlId would equal $GUI_EVENT_CLOSE, ; and @GUI_WinHandle would equal $hMainGUI ;MsgBox(0, "GUI Event", "You selected CLOSE! Exiting...") Exit EndFunc ;==>CLOSEButton Basically, see the comments in the code what I want to accomplish (when clicking OK):
      I want the red box to disappear completely (now bypassed by stating an image index that doesn't exist) I want to create the last row - "Three" - without any image whatsoever. The "OK" comments are the behavior I want to be able to use in certain cases. I'm also puzzled why the value "1" is originally indented while the value "2" is not?!
      If it matters, I'm running an older version of AutoIt (v3.3.10.2)