Jump to content

_GUICtrlListViewSort


 Share

Recommended Posts

If you try to sort an empty ControlListView, it causes an error due to creating an array with zero entries:

Local $a_lv[$items][$columns]

and

Local $a_lv[$items]

in both cases is $item = 0 because of the empty ListView.

Solution: (Look for the comment "; OLD: ..." )

Func _GUICtrlListViewSort(ByRef $h_listview, ByRef $v_descending, $i_sortcol, $s_Title = "", $s_text = "")
    Local $X, $Y, $b_desc, $columns, $items, $v_item, $temp_item
    If (StringLen($s_Title) == 0) Then
        $s_Title = WinGetTitle("")
    EndIf
    If (IsArray($v_descending)) Then
        $b_desc = $v_descending[$i_sortcol]
    Else
        $b_desc = $v_descending
    EndIf
    $columns = _GUICtrlListViewGetSubItemsCount($h_listview, $s_Title, $s_text)
    $items = _GUICtrlListViewGetItemCount($h_listview)
    For $X = 1 To $columns
        $temp_item = $temp_item & " |"
    Next
    $temp_item = StringTrimRight($temp_item, 1)
    If ($columns > 1) And ($items > 1) Then          ; OLD: If ($columns > 1) Then         => problem: If there are no items the next line exits with an error
        Local $a_lv[$items][$columns]
        For $X = 0 To UBound($a_lv) - 1 Step 1
            For $Y = 0 To UBound($a_lv, 2) - 1 Step 1
                $v_item = _GUICtrlListViewGetItemText($h_listview, $X, $Y, $s_Title, $s_text)
                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
        Next
        _GUICtrlListViewDeleteAllItems($h_listview)
        _ArraySort($a_lv, $b_desc, 0, 0, $columns, $i_sortcol)
        For $X = 0 To UBound($a_lv) - 1 Step 1
            GUICtrlCreateListViewItem($temp_item, $h_listview)
            For $Y = 0 To UBound($a_lv, 2) - 1 Step 1
                _GUICtrlListViewSetItemText($h_listview, $X, $Y, $a_lv[$X][$Y])
            Next
        Next
    ElseIf ($items > 1) Then                          ; OLD: Else                             => problem: same as above
        Local $a_lv[$items]
        For $X = 0 To UBound($a_lv) - 1 Step 1
            $v_item = _GUICtrlListViewGetItemText($h_listview, $X, -1, $s_Title, $s_text)
            If (StringIsFloat($v_item) Or StringIsInt($v_item)) Then
                $a_lv[$X] = Number($v_item)
            Else
                $a_lv[$X] = $v_item
            EndIf
        Next
        _GUICtrlListViewDeleteAllItems($h_listview)
        _ArraySort($a_lv, $b_desc)
        For $X = 0 To UBound($a_lv) - 1 Step 1
            GUICtrlCreateListViewItem(" ", $h_listview)
            _GUICtrlListViewSetItemText($h_listview, $X, 0, $a_lv[$X])
        Next
    EndIf
    If (IsArray($v_descending)) Then
        $v_descending[$i_sortcol] = Not $b_desc
    Else
        $v_descending = Not $b_desc
    EndIf
EndFunc  ;==>_GUICtrlListViewSort
Edited by SnowOfIce
Link to comment
Share on other sites

Looked it over decided in simple if at beginning of function

Func _GUICtrlListViewSort(ByRef $h_listview, ByRef $v_descending, $i_sortcol, $s_Title = "", $s_text = "")
    Local $X, $Y, $b_desc, $columns, $items, $v_item, $temp_item
    If _GUICtrlListViewGetItemCount($h_listview) Then
        If (StringLen($s_Title) == 0) Then
            $s_Title = WinGetTitle("")
        EndIf
        If (IsArray($v_descending)) Then
            $b_desc = $v_descending[$i_sortcol]
        Else
            $b_desc = $v_descending
        EndIf
        $columns = _GUICtrlListViewGetSubItemsCount($h_listview, $s_Title, $s_text)
        $items = _GUICtrlListViewGetItemCount($h_listview)
        For $X = 1 To $columns
            $temp_item = $temp_item & " |"
        Next
        $temp_item = StringTrimRight($temp_item, 1)
        If ($columns > 1) Then
            Local $a_lv[$items][$columns]
            For $X = 0 To UBound($a_lv) - 1 Step 1
                For $Y = 0 To UBound($a_lv, 2) - 1 Step 1
                    $v_item = _GUICtrlListViewGetItemText($h_listview, $X, $Y, $s_Title, $s_text)
                    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
            Next
            _GUICtrlListViewDeleteAllItems($h_listview)
            _ArraySort($a_lv, $b_desc, 0, 0, $columns, $i_sortcol)
            For $X = 0 To UBound($a_lv) - 1 Step 1
                GUICtrlCreateListViewItem($temp_item, $h_listview)
                For $Y = 0 To UBound($a_lv, 2) - 1 Step 1
                    _GUICtrlListViewSetItemText($h_listview, $X, $Y, $a_lv[$X][$Y])
                Next
            Next
        Else
            Local $a_lv[$items]
            For $X = 0 To UBound($a_lv) - 1 Step 1
                $v_item = _GUICtrlListViewGetItemText($h_listview, $X, -1, $s_Title, $s_text)
                If (StringIsFloat($v_item) Or StringIsInt($v_item)) Then
                    $a_lv[$X] = Number($v_item)
                Else
                    $a_lv[$X] = $v_item
                EndIf
            Next
            _GUICtrlListViewDeleteAllItems($h_listview)
            _ArraySort($a_lv, $b_desc)
            For $X = 0 To UBound($a_lv) - 1 Step 1
                GUICtrlCreateListViewItem(" ", $h_listview)
                _GUICtrlListViewSetItemText($h_listview, $X, 0, $a_lv[$X])
            Next
        EndIf
        If (IsArray($v_descending)) Then
            $v_descending[$i_sortcol] = Not $b_desc
        Else
            $v_descending = Not $b_desc
        EndIf
    EndIf
EndFunc  ;==>_GUICtrlListViewSort

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

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