Jump to content
Sign in to follow this  
Reinhardt1julian

Sort Listview Name / Surname

Recommended Posts

Reinhardt1julian

I have a list view (first two colums are Name and Surname). Now I want to sort the items by alphabet. It should sort the names from A to Z and if the Name is teh shame it should sort it by the surname, too.

I know about the _GUICtrlListView_SimpleSort() Function, but that only sorts one column.

My problem is, that I want to save a value with the number in the list view (first is number one, second is number two...) in a file so when I need to retreive that value I search for the number of the item in the file.

Thank you!

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian,

I posted this UDF a while ago which sorts exactly as you require. You would need to extract the contents of the ListView into an array, sort the array with the UDF and then reload the array into the ListView - which is a lot easier than it sounds, believe me! ;)

Let me know if I can help you get it all working. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Reinhardt1julian

@Melba23

Since I have not worked with Arraylists before I am not quite understanding your code. And I don't know what $aSortData needs to be. And for me it has a Syntax error (in the AMCS_Ex.au3) at line 20 ("Global $aSortData[][] = [ _") Character 19

Edited by Reinhardt1julian

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian,

I will work up an example for you so that you can see it in action. :)

And you get the error because you need the Beta to run that syntax. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian,

Here you go: ;)

#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include "ArrayMultiColSort.au3"

#include <Array.au3>

Global $aList[8][2] = [[7, 0], _
        ["Tom", "Cliffe"], _
        ["Dick", "Harries"], _
        ["Harry", "Thompson"], _
        ["Tom", "Williams"], _
        ["Dick", "Webb"], _
        ["Harry", "Manning"], _
        ["Tom", "Payne"]]

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

$cLV = GUICtrlCreateListView("Forename       |Name            ", 10, 10, 400, 300)
_LoadArrayToLV($cLV, $aList, 1)

$cSort = GUICtrlCreateButton("Sort", 10, 350, 80, 30)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cSort
            _Sort()
    EndSwitch
WEnd

Func _Sort()

    ; Read ListView content into an array
    Local $aArray = _ReadLVToArray($cLV)

    _ArrayDisplay($aArray, "Before sort")

    ; Multisort the array - both columns "Ascending"
    Local $aSortData[2][2] = [[0, 0], [1, 0]]
    _ArrayMultiColSort($aArray, $aSortData)

    _ArrayDisplay($aArray, "After sort")

    ; Now reload the array into the ListView
    _LoadArrayToLV($cLV, $aArray)

EndFunc

Func _LoadArrayToLV($cLV, $aArray, $iStart = 0)

    ; Delete current items
    _GUICtrlListView_DeleteAllItems($cLV)

    ; Load from array
    For $i = 0 + $iStart To UBound($aArray) - 1
        GUICtrlCreateListViewItem($aArray[$i][0] & "|" & $aArray[$i][1], $cLV)
    Next

EndFunc

Func _ReadLVToArray($cLV, $iStart = 0)

    Local $aLVArray = "", $aRow

    ; Get ListView row count
    Local $iRows = _GUICtrlListView_GetItemCount($cLV)
    ; Check for empty ListView with no count
    If $iRows + $iStart <> 0 Then
        ; Get ListView column count
        Local $iCols = _GUICtrlListView_GetColumnCount($cLV)
        ; Create 2D array to hold ListView content and add count - count overwritten if not needed
        Local $aLVArray[$iRows + $iStart][$iCols] = [[$iRows]]
        ; Read ListView content into array
        For $i = 0 To $iRows - 1
            ; Read the row content
            $aRow = _GUICtrlListView_GetItemTextArray($cLV, $i)
            For $j = 1 To $aRow[0]
                ; Add to the ListView content array
                $aLVArray[$i + $iStart][$j - 1] = $aRow[$j]
            Next
        Next
    EndIf
    ; Return array or empty string
    Return $aLVArray

EndFunc
Please ask if you any questions. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Reinhardt1julian

Thank you so much! Is it possible to copy it 1:1 (i know i have to change some variables) or do i have to modify some things? Doesn't look like it.

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian,

You should be able to paste those functions straight into your script. But you know where I am if you run into any problems. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Reinhardt1julian

Oh, does this also work if there are more colums than the two? But it should only use the first two :D It looks like it, but I want to make sure... And it's complicated to test right now, because first i have to edity my whole aready made script

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian'

 

does this also work if there are more colums than the two?

It certainly does - just increase the number of elements in the array. This is explained in the UDF header and shown in the UDF example - which is why I wrote them. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Reinhardt1julian

Yeah, but it should ignore those coulumn in the sorting process. Just sort it using the first two colums and ignore the rest, and put them back sortet into the arraylist. From the UDF example i understood that it uses all columns to sort it.

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian,

No, the [#][0] element defines the column to sort - so the example I gave you would only sort columns 0 and 1 even if there were more than 2. Using this syntax would sort columns 3 and 4 while ignoring 0 and 1:

Local $aSortData[2][2] = [[3, 0], [4, 0]]
Clearer now? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Reinhardt1julian

Mostly.

So it needs to be

Local $aSortData[6][6] = [[0, 0], [1, 0]]

If it has 6 columns but should only sort the first two?

BTW, thanks for sacrificing so much time for me :D I appreciate that!

Edited by Reinhardt1julian

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian,

No, just this: :)

Local $aSortData[2][2] = [[0, 0], [1, 0]]
You really need to learn about 2D arrays! ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Reinhardt1julian

Yeah I do....

But now it takes the LiswViewItem for example

Smith|John|Info|07700|900583|B2 4DB
|Birmingham

And puts it back in as

Smith|John

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian,

Please post the code you have and I will see how we can sort it. :)

Remember you did not mention anything about 6 columns when you started this thread - I solved the problem you stated, so moving the goalposts mid-thread is very likely to throw up problems. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Reinhardt1julian

I made an example code:

#include <ListViewConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <GuiListView.au3>
    #include <WindowsConstants.au3>
    $Form5 = GUICreate("Test", 674, 469, -1, -1)
    $List1 = GUICtrlCreateListView("Name|Surename|Info|Area Code|Telephone|Postal code|City", 8, 32, 353, 383, -1, BitOR($WS_EX_ACCEPTFILES,$LVS_EX_FULLROWSELECT))
    $Button4 = GUICtrlCreateButton("Sort", 368, 232, 75, 25)
    GUISetState(@SW_SHOW)
    GUICtrlCreateListViewItem("Parker|Peter|This is info|3418|2154626|B2 4DB|Birmingham", $List1)
    GUICtrlCreateListViewItem("Louis|Louis|Is a person|4583|7952461|B2 4DB|Birmingham", $List1)
    GUICtrlCreateListViewItem("Grey|Christian|Is a random name|3419|794413154|B2 4DB|Birmingham", $List1)
    GUICtrlCreateListViewItem("Williams|John|Is a common name|8465|35415164|B2 4DB|Birmingham", $List1)
    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button4
                _Sort($List1)
            EndSwitch
        WEnd


Func _Sort($listview)
    ; Read ListView content into an array
    Local $aArray = _ReadLVToArray($listview)
    ; Multisort the array - both columns "Ascending"
    Local $aSortData[2][2] = [[0, 0], [1, 0]]
    _ArrayMultiColSort($aArray, $aSortData)
    ; Now reload the array into the ListView
    _LoadArrayToLV($listview, $aArray)
EndFunc
Func _LoadArrayToLV($cLV, $aArray, $iStart = 0)
    ; Delete current items
    _GUICtrlListView_DeleteAllItems($cLV)
    ; Load from array
    For $i = 0 + $iStart To UBound($aArray) - 1
        GUICtrlCreateListViewItem($aArray[$i][0] & "|" & $aArray[$i][1], $cLV)
    Next
EndFunc
Func _ReadLVToArray($cLV, $iStart = 0)
    Local $aLVArray = "", $aRow
    ; Get ListView row count
    Local $iRows = _GUICtrlListView_GetItemCount($cLV)
    ; Check for empty ListView with no count
    If $iRows + $iStart <> 0 Then
        ; Get ListView column count
        Local $iCols = _GUICtrlListView_GetColumnCount($cLV)
        ; Create 2D array to hold ListView content and add count - count overwritten if not needed
        Local $aLVArray[$iRows + $iStart][$iCols] = [[$iRows]]
        ; Read ListView content into array
        For $i = 0 To $iRows - 1
            ; Read the row content
            $aRow = _GUICtrlListView_GetItemTextArray($cLV, $i)
            For $j = 1 To $aRow[0]
                ; Add to the ListView content array
                $aLVArray[$i + $iStart][$j - 1] = $aRow[$j]
            Next
        Next
    EndIf
    ; Return array or empty string
    Return $aLVArray
EndFunc
Func _ArrayMultiColSort(ByRef $aArray, $aSortData, $iStart = 0, $iEnd = 0)
    If UBound($aArray, 2) = 0 Then
        Return SetError(1, 1, "")
    EndIf
    ; 2D sort data
    If UBound($aSortData, 2) <> 2 Then
        Return SetError(1, 2, "")
    EndIf
    If UBound($aSortData) > UBound($aArray) Then
        Return SetError(1, 3)
    EndIf
    ; Start element
    If $iStart < 0 Then
        $iStart = 0
    EndIf
    If $iStart >= UBound($aArray) - 1 Then
        Return SetError(1, 4, "")
    EndIf
    ; End element
    If $iEnd <= 0 Or $iEnd >= UBound($aArray) - 1 Then
        $iEnd = UBound($aArray) - 1
    EndIf
    ; Sanity check
    If $iEnd <= $iStart Then
        Return SetError(1, 5, "")
    EndIf
    Local $iCurrCol, $iChunk_Start, $iMatchCol
    ; Sort first column
    __AMCS_SortChunk($aArray, $aSortData, 0, $aSortData[0][0], $iStart, $iEnd)
    If @error Then
        Return SetError(2, @extended, "")
    EndIf
    ; Now sort within other columns
    For $iSortData_Row = 1 To UBound($aSortData) - 1
        ; Determine column to sort
        $iCurrCol = $aSortData[$iSortData_Row][0]
        ; Create arrays to hold data from previous columns
        Local $aBaseValue[$iSortData_Row]
        ; Set base values
        For $i = 0 To $iSortData_Row - 1
            $aBaseValue[$i] = $aArray[$iStart][$aSortData[$i][0]]
        Next
        ; Set start of this chunk
        $iChunk_Start = $iStart
        ; Now work down through array
        For $iRow = $iStart + 1 To $iEnd
            ; Match each column
            For $k = 0 To $iSortData_Row - 1
                $iMatchCol = $aSortData[$k][0]
                ; See if value in each has changed
                If $aArray[$iRow][$iMatchCol] <> $aBaseValue[$k] Then
                    ; If so and row has advanced
                    If $iChunk_Start < $iRow - 1 Then
                        ; Sort this chunk
                        __AMCS_SortChunk($aArray, $aSortData, $iSortData_Row, $iCurrCol, $iChunk_Start, $iRow - 1)
                        If @error Then
                            Return SetError(2, @extended, "")
                        EndIf
                    EndIf
                    ; Set new base value
                    $aBaseValue[$k] = $aArray[$iRow][$iMatchCol]
                    ; Set new chunk start
                    $iChunk_Start = $iRow
                EndIf
            Next
        Next
        ; Sort final section
        If $iChunk_Start < $iRow - 1 Then
            __AMCS_SortChunk($aArray, $aSortData, $iSortData_Row, $iCurrCol, $iChunk_Start, $iRow - 1)
            If @error Then
                Return SetError(2, @extended, "")
            EndIf
        EndIf
    Next
EndFunc   ;==>_ArrayMultiColSort
Func __AMCS_SortChunk(ByRef $aArray, $aSortData, $iRow, $iColumn, $iChunkStart, $iChunkEnd)
    Local $aSortOrder
    ; Set default sort direction
    Local $iSortDirn = 1
    ; Need to prefix elements?
    If IsString($aSortData[$iRow][1]) Then
        ; Split elements
        $aSortOrder = StringSplit($aSortData[$iRow][1], ",")
        If @error Then
            Return SetError(1, 1, "")
        EndIf
        ; Add prefix to each element
        For $i = $iChunkStart To $iChunkEnd
            For $j = 1 To $aSortOrder[0]
                If $aArray[$i][$iColumn] = $aSortOrder[$j] Then
                    $aArray[$i][$iColumn] = StringFormat("%02i-", $j) & $aArray[$i][$iColumn]
                    ExitLoop
                EndIf
            Next
            ; Deal with anything that does not match
            If $j > $aSortOrder[0] Then
                $aArray[$i][$iColumn] = StringFormat("%02i-", $j) & $aArray[$i][$iColumn]
            EndIf
        Next
    Else
        Switch $aSortData[$iRow][1]
            Case 0, 1
                ; Set required sort direction if no list
                If $aSortData[$iRow][1] Then
                    $iSortDirn = -1
                Else
                    $iSortDirn = 1
                EndIf
            Case Else
                Return SetError(1, 2, "")
        EndSwitch
    EndIf
    ; Sort the chunk
    Local $iSubMax = UBound($aArray, 2) - 1
    __ArrayQuickSort2D($aArray, $iSortDirn, $iChunkStart, $iChunkEnd, $iColumn, $iSubMax)
    ; Remove any prefixes
    If IsString($aSortData[$iRow][1]) Then
        For $i = $iChunkStart To $iChunkEnd
            $aArray[$i][$iColumn] = StringTrimLeft($aArray[$i][$iColumn], 3)
        Next
    EndIf
EndFunc   ;==>__AMCS_SortChunk
Edited by Reinhardt1julian

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian,

That was easy to fix: :)

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

$Form5 = GUICreate("Test", 674, 469, -1, -1)
$List1 = GUICtrlCreateListView("Name|Surename|Info|Area Code|Telephone|Postal code|City", 8, 32, 353, 383, -1, BitOR($WS_EX_ACCEPTFILES, $LVS_EX_FULLROWSELECT))
$Button4 = GUICtrlCreateButton("Sort", 368, 232, 75, 25)
GUISetState(@SW_SHOW)
GUICtrlCreateListViewItem("Parker|Peter|This is info|3418|2154626|B2 4DB|Birmingham", $List1)
GUICtrlCreateListViewItem("Louis|Louis|Is a person|4583|7952461|B2 4DB|Birmingham", $List1)
GUICtrlCreateListViewItem("Grey|Christian|Is a random name|3419|794413154|B2 4DB|Birmingham", $List1)
GUICtrlCreateListViewItem("Williams|John|Is a common name|8465|35415164|B2 4DB|Birmingham", $List1)
GUICtrlCreateListViewItem("Parker|John|Is a common name|8465|35415164|B2 4DB|Birmingham", $List1)
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button4
            _Sort($List1)
    EndSwitch
WEnd


Func _Sort($listview)
    ; Read ListView content into an array
    Local $aArray = _ReadLVToArray($listview)
    ; Multisort the array - both columns "Ascending"
    Local $aSortData[2][2] = [[0, 0],[1, 0]]
    _ArrayMultiColSort($aArray, $aSortData)
    ; Now reload the array into the ListView
    _LoadArrayToLV($listview, $aArray)
EndFunc   ;==>_Sort
Func _LoadArrayToLV($cLV, $aArray, $iStart = 0)
    ; Delete current items
    _GUICtrlListView_DeleteAllItems($cLV)
    ; Load from array
    For $i = 0 + $iStart To UBound($aArray) - 1
        $sItem = ""
        For $j = 0 To UBound($aArray, 2) - 1 ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            $sItem &= $aArray[$i][$j] & "|"
        Next
        $sItem = StringTrimRight($sItem, 1)
        GUICtrlCreateListViewItem($sItem, $cLV)
    Next
EndFunc   ;==>_LoadArrayToLV
Func _ReadLVToArray($cLV, $iStart = 0)
    Local $aLVArray = "", $aRow
    ; Get ListView row count
    Local $iRows = _GUICtrlListView_GetItemCount($cLV)
    ; Check for empty ListView with no count
    If $iRows + $iStart <> 0 Then
        ; Get ListView column count
        Local $iCols = _GUICtrlListView_GetColumnCount($cLV)
        ; Create 2D array to hold ListView content and add count - count overwritten if not needed
        Local $aLVArray[$iRows + $iStart][$iCols] = [[$iRows]]
        ; Read ListView content into array
        For $i = 0 To $iRows - 1
            ; Read the row content
            $aRow = _GUICtrlListView_GetItemTextArray($cLV, $i)
            For $j = 1 To $aRow[0]
                ; Add to the ListView content array
                $aLVArray[$i + $iStart][$j - 1] = $aRow[$j]
            Next
        Next
    EndIf
    ; Return array or empty string
    Return $aLVArray
EndFunc   ;==>_ReadLVToArray
Func _ArrayMultiColSort(ByRef $aArray, $aSortData, $iStart = 0, $iEnd = 0)
    If UBound($aArray, 2) = 0 Then
        Return SetError(1, 1, "")
    EndIf
    ; 2D sort data
    If UBound($aSortData, 2) <> 2 Then
        Return SetError(1, 2, "")
    EndIf
    If UBound($aSortData) > UBound($aArray) Then
        Return SetError(1, 3)
    EndIf
    ; Start element
    If $iStart < 0 Then
        $iStart = 0
    EndIf
    If $iStart >= UBound($aArray) - 1 Then
        Return SetError(1, 4, "")
    EndIf
    ; End element
    If $iEnd <= 0 Or $iEnd >= UBound($aArray) - 1 Then
        $iEnd = UBound($aArray) - 1
    EndIf
    ; Sanity check
    If $iEnd <= $iStart Then
        Return SetError(1, 5, "")
    EndIf
    Local $iCurrCol, $iChunk_Start, $iMatchCol
    ; Sort first column
    __AMCS_SortChunk($aArray, $aSortData, 0, $aSortData[0][0], $iStart, $iEnd)
    If @error Then
        Return SetError(2, @extended, "")
    EndIf
    ; Now sort within other columns
    For $iSortData_Row = 1 To UBound($aSortData) - 1
        ; Determine column to sort
        $iCurrCol = $aSortData[$iSortData_Row][0]
        ; Create arrays to hold data from previous columns
        Local $aBaseValue[$iSortData_Row]
        ; Set base values
        For $i = 0 To $iSortData_Row - 1
            $aBaseValue[$i] = $aArray[$iStart][$aSortData[$i][0]]
        Next
        ; Set start of this chunk
        $iChunk_Start = $iStart
        ; Now work down through array
        For $iRow = $iStart + 1 To $iEnd
            ; Match each column
            For $k = 0 To $iSortData_Row - 1
                $iMatchCol = $aSortData[$k][0]
                ; See if value in each has changed
                If $aArray[$iRow][$iMatchCol] <> $aBaseValue[$k] Then
                    ; If so and row has advanced
                    If $iChunk_Start < $iRow - 1 Then
                        ; Sort this chunk
                        __AMCS_SortChunk($aArray, $aSortData, $iSortData_Row, $iCurrCol, $iChunk_Start, $iRow - 1)
                        If @error Then
                            Return SetError(2, @extended, "")
                        EndIf
                    EndIf
                    ; Set new base value
                    $aBaseValue[$k] = $aArray[$iRow][$iMatchCol]
                    ; Set new chunk start
                    $iChunk_Start = $iRow
                EndIf
            Next
        Next
        ; Sort final section
        If $iChunk_Start < $iRow - 1 Then
            __AMCS_SortChunk($aArray, $aSortData, $iSortData_Row, $iCurrCol, $iChunk_Start, $iRow - 1)
            If @error Then
                Return SetError(2, @extended, "")
            EndIf
        EndIf
    Next
EndFunc   ;==>_ArrayMultiColSort
Func __AMCS_SortChunk(ByRef $aArray, $aSortData, $iRow, $iColumn, $iChunkStart, $iChunkEnd)
    Local $aSortOrder
    ; Set default sort direction
    Local $iSortDirn = 1
    ; Need to prefix elements?
    If IsString($aSortData[$iRow][1]) Then
        ; Split elements
        $aSortOrder = StringSplit($aSortData[$iRow][1], ",")
        If @error Then
            Return SetError(1, 1, "")
        EndIf
        ; Add prefix to each element
        For $i = $iChunkStart To $iChunkEnd
            For $j = 1 To $aSortOrder[0]
                If $aArray[$i][$iColumn] = $aSortOrder[$j] Then
                    $aArray[$i][$iColumn] = StringFormat("%02i-", $j) & $aArray[$i][$iColumn]
                    ExitLoop
                EndIf
            Next
            ; Deal with anything that does not match
            If $j > $aSortOrder[0] Then
                $aArray[$i][$iColumn] = StringFormat("%02i-", $j) & $aArray[$i][$iColumn]
            EndIf
        Next
    Else
        Switch $aSortData[$iRow][1]
            Case 0, 1
                ; Set required sort direction if no list
                If $aSortData[$iRow][1] Then
                    $iSortDirn = -1
                Else
                    $iSortDirn = 1
                EndIf
            Case Else
                Return SetError(1, 2, "")
        EndSwitch
    EndIf
    ; Sort the chunk
    Local $iSubMax = UBound($aArray, 2) - 1
    __ArrayQuickSort2D($aArray, $iSortDirn, $iChunkStart, $iChunkEnd, $iColumn, $iSubMax)
    ; Remove any prefixes
    If IsString($aSortData[$iRow][1]) Then
        For $i = $iChunkStart To $iChunkEnd
            $aArray[$i][$iColumn] = StringTrimLeft($aArray[$i][$iColumn], 3)
        Next
    EndIf
EndFunc   ;==>__AMCS_SortChunk
I am just off to cook dinner now - any questions will have to wait until later this evening. ;)

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Melba23

Reinhardt1julian,

Now I am fed and watered, did you get it to work as you wanted? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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

    • ternal
      By ternal
      Hi,
      Recently I have had the need to do a sort and then do a second sort while the item of the first sort stays the same ( double sorting , first on column x then while column x is the same sort column y).
      I did not put much efffort into error checking but so far I did not need it.
      For my applications so far it works perfectly however if someone is willing I want to test this extensivly.
      If anyone has big lists of random stuff to sort could you try this out please?
      #include <Array.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArraySort_Double ; Description ...: ; Syntax ........: _ArraySort_Double (Byref $array[, $first_index = Default[, $second_index = Default[, $ascending = Default]]]) ; Parameters ....: $array - 2d array to sort. ; $first_index - [optional] first column to sort. Default is 0. ; $second_index - [optional] second column to sort. Default is 1. ; $ascending - [optional] ascending/descending. Default is 1. ; Return values .: 1 if no errors occured , -1 if errors occured ; Author ........: Ternal ; Remarks .......: Needs excessive testing. ; Related .......: _arraysort() ; =============================================================================================================================== Func _ArraySort_Double (byref $array, $first_index = Default, $second_index = Default, $ascending = Default) Local $temp_value Local $counter = 1 If UBound($array, $UBOUND_DIMENSIONS) <> 2 Then MsgBox(0, "error", "error") return -1 EndIf If $first_index = Default Then $first_index = 0 If $second_index = Default Then $second_index = 1 If $ascending = Default Then $ascending = 1 _ArraySort($array, $ascending, 0, 0, $first_index); you can alter settings of primary sort here If @error Then MsgBox(0, "error", @error) return -1 EndIf $temp_value = $array[0][$first_index] For $x = 1 to UBound($array, 1) - 1 If Mod( $x, 10000) = 0 Then ConsoleWrite("at " & $x & " of a total : " & UBound($array, 1) & @CRLF) If $array[$x][$first_index] = $temp_value Then $counter+= 1 If $x = UBound($array, 1) - 1 Then; do last line here(if last line is not a new item) _ArraySort($array, $ascending, $x - $counter, $x, $second_index);you can alter settings of secondary sort here(don't forget to place line 34 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf EndIf Else If $counter > 0 Then ;at least 2 of the same _ArraySort($array, $ascending, $x - $counter, $x - 1, $second_index);you can alter settings of secondary sort here(don't forget to place line 29 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf $counter = 1 EndIf EndIf $temp_value = $array[$x][$first_index] Next Return 1 EndFunc Kind regards, Ternal
    • Broihon
      By Broihon
      Hey guys,

      I ran into a problem when I wanted to delete items of a listview using the delete key. My approach is to register my own WndProc for the listview and then filter the $WM_GETDLGCODE msg and then call the original WndProc.
      That works like a charm. The problem is that when the listview gets redrawn it somehow ends up in an infinite loop. The listview is suddenly emtpy and I can't interact with any controls of the GUI at all. Here's the code:
       
      #include <GUIConstantsEx.au3> #include <GUIListView.au3> #include <WinAPI.au3> $h_GUI = GUICreate("Test", 200, 200, 350, 350) $h_LV = GUICtrlCreateListView("Col 1|Col 2|Col 3", 0, 0, 200, 200) _GUICtrlListView_AddItem($h_LV, "") _GUICtrlListView_AddSubItem($h_LV, 0, "bla0", 0) _GUICtrlListView_AddSubItem($h_LV, 0, "bla0", 1) _GUICtrlListView_AddSubItem($h_LV, 0, "bla0", 2) _GUICtrlListView_AddItem($h_LV, "") _GUICtrlListView_AddSubItem($h_LV, 1, "bla1", 0) _GUICtrlListView_AddSubItem($h_LV, 1, "bla1", 1) _GUICtrlListView_AddSubItem($h_LV, 1, "bla1", 2) _GUICtrlListView_AddItem($h_LV, "") _GUICtrlListView_AddSubItem($h_LV, 2, "bla2", 0) _GUICtrlListView_AddSubItem($h_LV, 2, "bla2", 1) _GUICtrlListView_AddSubItem($h_LV, 2, "bla2", 2) _GUICtrlListView_AddItem($h_LV, "") _GUICtrlListView_AddSubItem($h_LV, 3, "bla3", 0) _GUICtrlListView_AddSubItem($h_LV, 3, "bla3", 1) _GUICtrlListView_AddSubItem($h_LV, 3, "bla3", 2) $h_LV_NewWndProc = DllCallbackRegister("WndProc_LV", "int", "hwnd;uint;wparam;lparam") $g_LV_OldWndProc = _WinAPI_SetWindowLong(GUICtrlGetHandle($h_LV), $GWL_WNDPROC, DllCallbackGetPtr($h_LV_NewWndProc)) GUISetState(@SW_SHOW) Func WndProc_LV($hWnd, $uMsg, $wParam, $lParam) Return _WinAPI_CallWindowProc($g_LV_OldWndProc, $hWnd, $uMsg, $wParam, $lParam) EndFunc ;==>WndProc_GUI Do Until GUIGetMsg() = $GUI_EVENT_CLOSE DllCallbackFree($h_LV_NewWndProc) As you can see I'm doing nothing in the WndProc of the listview. I'm simply calling the original WndProc. This still "freezes" when I mess with the width of the columns or in the original project when I add more items that they don't fit in the listview and I have to scroll. If I don't do that it's working fine.
    • c.haslam
      By c.haslam
      I have
      Local $idListview = GUICtrlCreateListView("",8,8,@DesktopWidth/2-16,@DesktopHeight-150,$LVS_SHOWSELALWAYS, _ $LVS_EX_INFOTIP) Local $hListView = GUICtrlGetHandle($idListview) _GUICtrlListView_InsertColumn($hListview, 0, "Filename", 400) _GUICtrlListView_InsertColumn($hListview, 1, "Ext", 50) _GUICtrlListView_InsertColumn($hListview, 2, "Size",70) _GUICtrlListView_InsertColumn($hListview, 3, 'Date time',100) _GUICtrlListView_InsertColumn($hListview, 4, "Path", 385) _GUICtrlListView_InsertColumn($hListview, 5, "sizeInt", 0) _GUICtrlListView_JustifyColumn($hListview, $kSize,1) ; right align then further on
      While True $sFnamExt = FileFindNextFile($iSrch) If @error Then ExitLoop EndIf $sAtts = FileGetAttrib($sPath&'\'&$sFnamExt) If StringInStr($sAtts,'D') Then If $sFnamExt<>'$RECYCLE.BIN' Then $sDirs &= '?'&$sPath&'\'&$sFnamExt EndIf Else $p = StringInStr($sFnamExt,'.',0,-1) ; last If $p=0 Then $sFnam = $sFnamExt $sExt = '' Else $sFnam = StringLeft($sFnamExt,$p-1) $sExt = StringTrimLeft($sFnamExt,$p) EndIf _GUICtrlListView_AddItem($hListview,$sFnam,-1,_GUICtrlListView_GetItemCount($hListview)+1000) _GUICtrlListView_AddSubItem($hListview,$nItem,$sExt,$kExt) $nSize = FileGetSize($sPath&'\'&$sFnamExt) $sSize = AddThousandsSeparator($nsize) _GUICtrlListView_AddSubItem($hListview,$nItem,$sSize,$kSize) _GUICtrlListView_AddSubItem($hListview,$nItem,$nsize,$kSizeInt) $nTotBytes += $nSize $a1 = FileGetTime($sPath&'\'&$sFnamExt,$FT_MODIFIED,$FT_ARRAY) $t = $a1[0]&'-'&$a1[1]&'-'&$a1[2]&' '&$a1[3]&':'&$a1[4] _GUICtrlListView_AddSubItem($hListview,$nItem,$t,$kDateTime) _GUICtrlListView_AddSubItem($hListview,$nItem,$sPath,$kPath) If $gSQL Then $s = "Insert into tbl values ("&_SQLite_Escape($sFnam)&","&_SQLite_Escape($sExt)&",'"& _ $sSize&"','"& _ $t&"',"&_SQLite_Escape($sPath)&","&$nSize&")" _SQLite_Exec(-1,$s) If @error Then MsgBox(0,@ScriptLineNumber,_SQLite_ErrMsg()) EndIf EndIf EndIf WEnd FileClose($iSrch) You will see that I heeded the advice in Help > _GUICtrlListView_AddItem: "As AutoIt uses the $iParam parameter to store the controlID of native-created ListView items, this value should be set sufficiently high for UDF-created items to avoid possible conflict with any existing controls - a starting value of 1000 is recommended."
      (It's unfortunate that the Example does not heed this advice. OK, it doesn't need to because there are no other controls, but still --- it would help neophytes if it did. Also to me _GUICtrlListView_AddItem is not native because it is a UDF. Confused?)
      My script then does a sort using SQLite, and updates the ListView:
      Local $hQuery Local $colNames = ['fnam','ext','nsize','dateTime','path','SizeInt'] Local $s = "Select * FROM tbl ORDER BY "&$colNames[$ncol]&';' _SQLite_Query(-1,$s, $hQuery) If @error Then MsgBox(0,@ScriptLineNumber,_SQLite_ErrMsg()) EndIf Local $aRow[$kSizeInt+1] Local $iItem=-1 While _SQLite_FetchData($hQuery, $aRow, False, False) = $SQLITE_OK ; Read Out the next Row If @error Then MsgBox(0,@ScriptLineNumber,_SQLite_ErrMsg()) EndIf $iItem += 1 For $i = $kFnam To $kSizeInt _GUICtrlListView_SetItem($hListview,$aRow[$i],$iItem,$i,Default) Next WEnd This works, but I had earlier coded
      _GUICtrlListView_SetItem($hListview,$aRow[$i],$iItem,$i,Default,$iItem+1000) because the same advice is in the Help for this function.
      So my care in specifying $param back-fired! What am I not understanding?
      The only difference in what works is that the $param parameter is defaulted.
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am building a management for the company I work with, and I just imported a real amount of rows ( about 29000 ), in my SQLite DB.
      The thing I am not understanding, is the time that the script takes to build this amount of rows in the ListView.
      I didn't measure it, but I think it took 2 minutes or so to create each ListView item...
      It is normal that it takes so much time?
      What can I do to improve the creation of the items?

      Here's the code I am using to query and to create ListView items...
      ; Articles ListView: Global $lvwArticles = GUICtrlCreateListView("ID|Fornitore|Codice|Descrizione|EU|Prezzo|Sconto Applicato|Note", 14, 87, 1507, 660, BitOR($GUI_SS_DEFAULT_LISTVIEW,$LVS_SORTASCENDING,$LVS_SORTDESCENDING), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT)) ; Query $strQuery = "SELECT * FROM ARTICOLI;" ; Query Execution _SQLite_GetTable2d($objDatabase, $strQuery, $arrResult, $intRows, $intColumns) If @error Then ; Error Handling Else ; Cleaning the ListView _GUICtrlListView_DeleteAllItems($lvwArticles) If @error Then ; Error Handling Else ; No records in the Table If UBound($arrResult) < 2 Then ; Error Handling Else _GUICtrlListView_BeginUpdate($lvwArticles) For $intCounter = 1 To UBound($arrResult) - 1 $strListViewItem = $arrResult[$intCounter][0] & "|" & _ $arrResult[$intCounter][1] & "|" & _ $arrResult[$intCounter][2] & "|" & _ $arrResult[$intCounter][3] & "|" & _ $arrResult[$intCounter][4] & "|" & _ $arrResult[$intCounter][5] & "|" & _ $arrResult[$intCounter][6] & "|" & _ $arrResult[$intCounter][7] $objListViewItem = GUICtrlCreateListViewItem($strListViewItem, $lvwArticles) Next _GUICtrlListView_EndUpdate($lvwArticles) EndIf EndIf EndIf Thanks in advance


      Best Regards.
    • TimRude
      By TimRude
      The help text for the _ArrayDisplay function says: "Clicking on a column header sort it."
      I'm wondering why the sorting of hex numbers (specifically window handles like 0x12345678) is so wonky.
      Sample script:
      #include <Array.au3> Local $aList = WinList() _ArrayDisplay($aList) Clicking on the Col1 header to sort by window handle doesn't properly sort the list.
      As a workaround, I'm sorting the array by window handle before displaying it using _ArraySort. But I still wonder what's up with the ListView sorting.
      Workaround:
      #include <Array.au3> Local $aList = WinList() _ArraySort($aList, 0, 1, 0, 1) _ArrayDisplay($aList)  
×