Jump to content

Sort Function Converts a Number Stored as String to Integer?


zbatev
 Share

Recommended Posts

Hi All,

I salvaged this function from help(Simple Sort using _GUICtrlListView_SimpleSort), notice the value '09857' stored as $aString when you click on any column to sort, it removes the preceding zero and the value 09857 becomes 9857.

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include
#include
#include
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
_Main()
Func _Main()
local $aString
GUICreate("ListView SimpleSort", 400, 300)
$aString = "09857"
$hListView = GUICtrlCreateListView("col1|col2|col3", 2, 2, 394, 268)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlCreateListViewItem("line4|5|more_a", $hListView)
GUICtrlCreateListViewItem("line5|4.50 |more_c", $hListView)
GUICtrlCreateListViewItem("line5|4.0 |more_c", $hListView)
GUICtrlCreateListViewItem("line3|23|more_e", $hListView)
GUICtrlCreateListViewItem("line2|0.34560 |more_d", $hListView)
GUICtrlCreateListViewItem("line1|1.0 |more_b", $hListView)
GUICtrlCreateListViewItem("line1|" & $aString & "|more_b", $hListView)
GUICtrlCreateListViewItem("line1|10|more_b", $hListView)
_GUICtrlListView_SetColumnWidth($hListView, 0, 75)
_GUICtrlListView_SetColumnWidth($hListView, 1, 75)
_GUICtrlListView_SetColumnWidth($hListView, 2, 75)
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($hListView)]
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc   ;==>_Main
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
$hWndListView = $hListView
If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
  Case $hWndListView
   Switch $iCode
    Case $LVN_COLUMNCLICK ; A column was clicked
     $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
     _DebugPrint("$LVN_COLUMNCLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
       "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
       "-->Code:" & @TAB & $iCode & @LF & _
       "-->Item:" & @TAB & DllStructGetData($tInfo, "Item") & @LF & _
       "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
       "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
       "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
       "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
       "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
       "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
       "-->Param:" & @TAB & DllStructGetData($tInfo, "Param"))
     _GUICtrlListView_SimpleSort($hWndListView, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
     ; No return value
    Case $LVN_KEYDOWN ; A key has been pressed
     $tInfo = DllStructCreate($tagNMLVKEYDOWN, $ilParam)
     _DebugPrint("$LVN_KEYDOWN" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
       "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
       "-->Code:" & @TAB & $iCode & @LF & _
       "-->VKey:" & @TAB & DllStructGetData($tInfo, "VKey") & @LF & _
       "-->Flags:" & @TAB & DllStructGetData($tInfo, "Flags"))
     ; No return value
    Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
     $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
     _DebugPrint("$NM_CLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
       "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
       "-->Code:" & @TAB & $iCode & @LF & _
       "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
       "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
       "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
       "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
       "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
       "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
       "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
       "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
       "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
     ; No return value
    Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
     $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
     _DebugPrint("$NM_DBLCLK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
       "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
       "-->Code:" & @TAB & $iCode & @LF & _
       "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
       "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
       "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
       "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
       "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
       "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
       "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
       "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
       "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
     ; No return value
    Case $NM_KILLFOCUS ; The control has lost the input focus
     _DebugPrint("$NM_KILLFOCUS" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
       "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
       "-->Code:" & @TAB & $iCode)
     ; No return value
    Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the right mouse button
     $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
     _DebugPrint("$NM_RCLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
       "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
       "-->Code:" & @TAB & $iCode & @LF & _
       "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
       "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
       "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
       "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
       "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
       "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
       "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
       "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
       "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
     ;Return 1 ; not to allow the default processing
     Return 0 ; allow the default processing
    Case $NM_RDBLCLK ; Sent by a list-view control when the user double-clicks an item with the right mouse button
     $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
     _DebugPrint("$NM_RDBLCLK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
       "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
       "-->Code:" & @TAB & $iCode & @LF & _
       "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
       "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
       "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
       "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
       "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
       "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
       "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
       "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
       "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
     ; No return value
    Case $NM_RETURN ; The control has the input focus and that the user has pressed the ENTER key
     _DebugPrint("$NM_RETURN" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
       "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
       "-->Code:" & @TAB & $iCode)
     ; No return value
    Case $NM_SETFOCUS ; The control has received the input focus
     _DebugPrint("$NM_SETFOCUS" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
       "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
       "-->Code:" & @TAB & $iCode)
     ; No return value
   EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Func _DebugPrint($s_text, $line = @ScriptLineNumber)
ConsoleWrite( _
   "!===========================================================" & @LF & _
   "+======================================================" & @LF & _
   "-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_text & @LF & _
   "+======================================================" & @LF)
EndFunc   ;==>_DebugPrint
Edited by zbatev
Link to comment
Share on other sites

Looking through the SimpleSort function I found this little gem:

For $Y = 0 To UBound($a_lv, 2) - 2 Step 1
    $v_item = StringStripWS(_GUICtrlListView_GetItemText($hWnd, $x, $Y), 2)
    If (StringIsFloat($v_item) Or StringIsInt($v_item)) Then
        $a_lv[$x][$Y] = Number($v_item)
    Else
        $a_lv[$x][$Y] = $v_item
    EndIf
Next

The problem is this same data is then placed back into the listview after sorting. A UDF can't mangle data types like this. If the function insists on the data conversion for sorting purposes, then it should be using some kind of parallel array to sort, then place the original data back into the listview.

You should probably open a trac ticket.

Edited by wraithdu
Link to comment
Share on other sites

Looking at that chunk of code I noticed it's also stripping whitespace from the end of the text it's getting from the listview. I'm not sure why it was implemented that way, because I'm sure there are some people that have a LV with elements that have spaces after the text in a sub-item for whatever reason.

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

I also noticed that. what I did was just to replicate the GuiListView.au3 and removed the lines that converts numbers stored as text to integer and the line that strips white spaces.

I have to do this because I'm using those values to look up in a database so a slight change would not return a value whenever I query something.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...