Jump to content

_ArraySort Problem with mixed values (numbers, strings)


 Share

Recommended Posts

Hey folks,

I have a quite simple but weird problem with _ArraySort. Check it out:

#include <Array.au3>

Local $avArray[5][2] = [ _
        [15, 20], _
        ['24', 32], _
        [8, 16], _
        [3, 35], _
        ['10,5', 19]]

_ArrayDisplay($avArray, "$avArray BEFORE _ArraySort()")
_ArraySort($avArray, 1, 0, 0, 0)
_ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 0")

The leftmost column should be sorted descending. But the strings make some problems.

Does someone have a quick solution?

Best Wishes

error471

Link to comment
Share on other sites

  • Moderators

error471,

Force all the elements into the Number datatype before sorting:

#include <Array.au3>

Local $avArray[5][2] = [ _
        [15, 20], _
        ['24', 32], _
        [8, 16], _
        [3, 35], _
        ['10,5', 19]]

_ArrayDisplay($avArray, "$avArray BEFORE _ArraySort()")

For $i = 0 To UBound($avArray) - 1
    $avArray[$i][0] = Number($avArray[$i][0])
Next

_ArraySort($avArray, 1, 0, 0, 0)
_ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 0")

By default _ArraySort sorts on character ASCII codes, hence the apparent errors.

M23

Edited by Melba23
Typo

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

It looks like it's working as expected to me.  Since '24' is wrapped in quotes (as is '10,5') it is interpreted as a string.  I can understand that not showing the quotes in the arraydisplay output is confusing, but it is bahaving as expected based on the datatypes (ASCII codes...thanks Melba)--notice the numbers sorted and grouped as are the strings.

 

edit:  I was going to suggest what Melba has, except I would've enforced the "number" datatype during array population.  It's probably more important to make sure the datatype is accurate when the script needs to "act' on the data versus when the data is produced.  Garbage in, garbage out.

Edited by spudw2k
Link to comment
Share on other sites

  • Moderators

spudw2k,

5 minutes ago, spudw2k said:

Garbage in, garbage out

I used to know the guy who coined that expression - the much-missed Wilf Hey. His "Wilf's Worshop" series in the PCPlus magazine was required reading when I was younger.  I even won a prize in one of his weekly competitions - this particular one was to code for Turing machine.  My prize was "FromBat", a tool which produced executables from essentially DOS batch file code with a lot of added functions - a sort of proto-DOS-based- Autoit I suppose. Happy days......

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

error471,

My pleasure, as always.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

error471,

That is because you are using the continental comma ( , ) to indicate a decimal and not the expected period ( . ). I always thought that Windows and AutoIt used the settings for the local machine, so perhaps you  have not set this correctly. If this is not the case then you will need to convert the separator at the same time as converting the datatype:

$avArray[$i][0] = Number(StringReplace($avArray[$i][0], ", ", "."))

Anyway, I thought this was for a bakery - how come you deal in decimals?

M23

Edited by Melba23
Typo

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

error471,

Quote

That comma/period changing will cause a lot of unnecessary code

Not necessarily. As you use my UDF, you can use the array returned by _GUIListViewEx_EditOnClick to remove the commas as they are entered. Give me a moment to work up an example.

M23

Edit: Here you are:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#include "GUIListViewEx_Mod.au3"

$hGUI = GUICreate("Test", 500, 500)

; Create ListView
$cLV_1 = GUICtrlCreateListView("Zero Column|One Column|Two Column|Three Column", 10, 10, 480, 280, BitOR($LVS_SINGLESEL, $LVS_SHOWSELALWAYS))
_GUICtrlListView_SetExtendedListViewStyle($cLV_1, $LVS_EX_FULLROWSELECT)
For $i = 0 To 0
    _GUICtrlListView_SetColumnWidth($cLV_1, $i, 100)
Next

; Create array and fill listview
Global $aLVArray_1[6][4]
For $i = 0 To 5
    $sData = "Item " & $i & "-0"
    $aLVArray_1[$i][0] = $sData
    For $j = 1 To 3
        $sData &= "|SubItem " & $i & "-" & $j
        $aLVArray_1[$i][$j] = "SubItem " & $i & "-" & $j
    Next
    GUICtrlCreateListViewItem($sData, $cLV_1)
Next

; Initiate ListView - edit on click all columns
$iLVIndex_1 = _GUIListViewEx_Init($cLV_1, $aLVArray_1, 0, 0, True, 2)

_GUIListViewEx_MsgRegister()

GUISetState()

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; Allow edit on double click
    $aRet = _GUIListViewEx_EditOnClick()
    If IsArray($aRet) Then
        ; Loop through the changes
        For $i = 1 to $aRet[0][0]
            ; Look for commas
            If StringinStr($aRet[$i][3], ",") Then
                ; replace with decimal points
                $sReplace = Number(StringReplace($aRet[$i][3], ",", "."))
                ; programatically set the content
                _GUIListViewEx_ChangeItem($iLVIndex_1, $aRet[$i][0], $aRet[$i][1], $sReplace)
            EndIf
        Next
    EndIf

WEnd

Not too difficult.

Edited by Melba23
Added Number forcing too

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

error471,

My UDF saves all edited items as strings. You are running into one of the "features" of AutoIt - variables are not typed and so can be either strings or numbers. Normally this makes for easy coding, but can bite you if you are not careful. It was a design decision by Jon when he first started - personally I think it was correct, even if it catches experienced coders out from time to time (speaking from experience here)!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

error471,

Did you see my example code above? Using something like that makes it really easy to get the input in a suitable format immediately - you could even force the Number datatype at that point as well as I have now done.

M23

Edited by Melba23
Amended earlier code

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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