Jump to content

RegisterListViewSort by size


Terenz
 Share

Go to solution Solved by FireFox,

Recommended Posts

Hello guys,

I'm checking the example on the help ( for improve my knowledge if i can ) and i'm on the GUICtrlRegisterListViewSort. Is a very cool function, much different from the simple sort.

Maybe exist somewere an script on the forum but i don't have found it, there is a way to sort by file size?

I'm refering to the Example 1:

https://www.autoitscript.com/autoit3/docs/functions/GUICtrlRegisterListViewSort.htm

I have on column 2 this value:

100 byte

1 GB

1,15 MB

150 KB

So first i think i need to StringSpit the space for have the numeric value and "Byte-KB-MB-GB"

Then based on that result multiply the number for the suffix, like

100 * 1(Byte) = 100

1 * 1073741824(GB) = 1073741824

1,15 * 1048576(MB) = 1205862,4

150 * 1024(KB) = 153600

Now i can sort by number, but how?

Ok, i'm lost about compare and set the Return value -1 or 0 or 1, the real sorting part.

Please if is possible a little hand :D

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Solution

Hi,

I have edited an example, here you go :

#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.9.21 (Beta)
    Author:         myName

    Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

; sorting 3 column's different

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

Global $nCurCol = -1
Global $nSortDir = 1
Global $bSet = 0
Global $nCol = -1

Example()

Func Example()
    Local $lv, $msg

    GUICreate("Test", 300, 200)

    $lv = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180)
    GUICtrlRegisterListViewSort(-1, "LVSort") ; Register the function "SortLV" for the sorting callback

    GUICtrlCreateListViewItem("ABC|666|100 byte", $lv)
    GUICtrlSetImage(-1, "shell32.dll", 7)
    GUICtrlCreateListViewItem("DEF|444|1 GB", $lv)
    GUICtrlSetImage(-1, "shell32.dll", 12)
    GUICtrlCreateListViewItem("CDE|444|1,15 MB", $lv)
    GUICtrlSetImage(-1, "shell32.dll", 3)
    GUICtrlCreateListViewItem("ZXA|888|150 KB", $lv)
    GUICtrlSetImage(-1, "shell32.dll", 13)
    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $lv
                $bSet = 0
                $nCurCol = $nCol
                GUICtrlSendMsg($lv, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($lv), 0)
                DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($lv), "int", 0, "int", 1)
        EndSwitch
    WEnd

    GUIDelete()
EndFunc   ;==>Example

Func OrderSizes(ByRef $val1, ByRef $val2)
    Local $aVals[2] = [$val1, $val2]

    Local $aSizes[4] = ["byte", "KB", "MB", "GB"]

    For $j = 0 To 1
        Local $a2 = StringSplit($aVals[$j], " ", 2)

        For $i = 0 To UBound($aSizes) -1
            If $a2[1] = $aSizes[$i] Then
                $aVals[$j] = $a2[0] * 1024 ^ $i
                ExitLoop
            EndIf
        Next
    Next

    $val1 = $aVals[0]
    $val2 = $aVals[1]
EndFunc

; Our sorting callback funtion
Func LVSort($hWnd, $nItem1, $nItem2, $nColumn)
    Local $val1, $val2, $nResult

    ; Switch the sorting direction
    If $nColumn = $nCurCol Then
        If Not $bSet Then
            $nSortDir = $nSortDir * -1
            $bSet = 1
        EndIf
    Else
        $nSortDir = 1
    EndIf
    $nCol = $nColumn

    $val1 = GetSubItemText($hWnd, $nItem1, $nColumn)
    $val2 = GetSubItemText($hWnd, $nItem2, $nColumn)

    ; If it is the 3rd colum (column starts with 0) then compare the dates
    If $nColumn = 2 Then
        OrderSizes($val1, $val2)
    EndIf

    $nResult = 0 ; No change of item1 and item2 positions

    If $val1 < $val2 Then
        $nResult = -1 ; Put item2 before item1
    ElseIf $val1 > $val2 Then
        $nResult = 1 ; Put item2 behind item1
    EndIf

    $nResult = $nResult * $nSortDir

    Return $nResult
EndFunc   ;==>LVSort

; Retrieve the text of a listview item in a specified column
Func GetSubItemText($nCtrlID, $nItemID, $nColumn)
    Local $stLvfi = DllStructCreate("uint;ptr;int;int[2];int")
    Local $nIndex, $stBuffer, $stLvi, $sItemText

    DllStructSetData($stLvfi, 1, $LVFI_PARAM)
    DllStructSetData($stLvfi, 3, $nItemID)

    $stBuffer = DllStructCreate("char[260]")

    $nIndex = GUICtrlSendMsg($nCtrlID, $LVM_FINDITEM, -1, DllStructGetPtr($stLvfi));

    $stLvi = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int")

    DllStructSetData($stLvi, 1, $LVIF_TEXT)
    DllStructSetData($stLvi, 2, $nIndex)
    DllStructSetData($stLvi, 3, $nColumn)
    DllStructSetData($stLvi, 6, DllStructGetPtr($stBuffer))
    DllStructSetData($stLvi, 7, 260)

    GUICtrlSendMsg($nCtrlID, $LVM_GETITEMA, 0, DllStructGetPtr($stLvi));

    $sItemText = DllStructGetData($stBuffer, 1)

    $stLvi = 0
    $stLvfi = 0
    $stBuffer = 0

    Return $sItemText
EndFunc   ;==>GetSubItemText
Br, FireFox. Edited by FireFox
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...