Sign in to follow this  
Followers 0
JohnRichard

How to arrange Listview items from latest to oldest

7 posts in this topic

Good day to all AutoIT users. I'm having problem how to arrange the listview items. I'm trying to sort it by date. It can be sorted by using the Function WM_NOTIFY available here on the site. But is sorts the listview by numbers like 1, 10, 2, 20. I wanted to be sorted from the latest list to the oldest. The list comes from a notepad something like the one below. Is this possible?

15/06/2009|2:34:41 PM|2:34:41 PM|00:00:01| PC1 |10.110.5.21

16/06/2009|2:43:15 PM|2:43:18 PM|00:00:03| PC2 |10.110.5.22

21/06/2009|2:44:05 PM|2:44:09 PM|00:00:04| PC3 |10.110.5.23

30/06/2009|2:50:50 PM|2:50:51 PM|00:00:01| PC4 |10.110.5.24

01/07/2009|2:52:50 PM|2:53:26 PM|00:00:35| PC5 |10.110.5.25

10/07/2009|2:54:42 PM|2:55:08 PM|00:00:25| PC6 |10.110.5.26

Share this post


Link to post
Share on other sites



Try to convert each time part to YYYYMMDDHHMMSS time format and you could sort array by time ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Try to convert each time part to YYYYMMDDHHMMSS time format and you could sort array by time ! Posted Image

hi wakillon. thanks for the tip. i'll give it a try. :)

Share this post


Link to post
Share on other sites

If you're using _GUICtrlListView_RegisterSortCallBack and _GUICtrlListView_SortItems then you can set the parameter for _GUICtrlListView_RegisterSortCallBack that treats numbers as strings rather than as numbers. That might fix the issue for you.


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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hello JohnRichard,

here's a little try to solve or problem:

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <File.au3>
;#include <array.au3>

Local $tZeit, $s1, $s2, $aFile
$main = GUICreate("Lv-Sort", 600, 400)
$listview1 = GUICtrlCreateListView("Date|Time Logged In|Last Responce|Idle|PC-No|IP|oCol", 10, 10, 590, 390, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS), BitOR($LVS_EX_FULLROWSELECT, $WS_EX_CLIENTEDGE))
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_LV_ALTERNATE)
GUICtrlSetBkColor(-1, 0xE6E6FA) ; BackgrondColor odd
_FileReadToArray(@ScriptDir & "\LVtoSort.TXT", $aFile)
If Not @error Then
    For $i = 1 To $aFile[0]
        GUICtrlCreateListViewItem($aFile[$i] & "|" & $i, $listview1)
        GUICtrlSetBkColor(-1, 0xcccccc) ; BackgrondColor even
    Next
EndIf
_GUICtrlListView_SetColumnWidth($listview1, 0, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview1, 1, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview1, 2, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview1, 3, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview1, 4, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview1, 5, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($listview1, 6, 0);This Column is the original Row-Nmber

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;<=========================================== Sortieren

GUISetState()
$first = True
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $listview1
    If Not IsHWnd($listview1) Then $hWndListView = GUICtrlGetHandle($listview1)

    $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 ; Click on column
                    _GUICtrlListView_BeginUpdate($listview1)
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    Local $iColNo = DllStructGetData($tInfo, "SubItem")
                    $j = _GUICtrlListView_GetItemCount($listview1)
                    $c = _GUICtrlListView_GetColumnCount($listview1)
                    Local $aSortArray[$j]
                    For $i = 0 To $j - 1
                        $aItem = _GUICtrlListView_GetItemTextArray($listview1, $i)
                        Switch $iColNo
                            Case 0,1
                                $aSplitDate = StringSplit($aItem[1], "/")
                                $aSplitTime = _SplitTime($aItem[2])
                                $sToSort = $aSplitDate[3] & $aSplitDate[2] & $aSplitDate[1] & $aSplitTime[1] & $aSplitTime[2] & $aSplitTime[3] & "\|\" & $aItem[7] & "\|\"
                            Case 2,3
                                $aSplitTime = _SplitTime($aItem[3])
                                $sToSort = $aItem[4] & "\|\" & $aItem[7] & "\|\"
                            Case 4
                                $sToSort = $aItem[5] & "\|\" & $aItem[7] & "\|\"
                            Case 5
                                $sToSort = $aItem[6] & "\|\" & $aItem[7] & "\|\"
                        EndSwitch
                        For $k = 1 To $c - 1
                            $sToSort &= $aItem[$k]
                            If $k <> $c - 1 Then $sToSort &= "|"
                        Next
                        $aSortArray[$i] = $sToSort
                    Next
                    _arraySort($aSortArray)
                    _GUICtrlListView_DeleteAllItems($listview1)
                    For $i = 0 To $j - 1
                        $aTemp = StringSplit($aSortArray[$i],"\|\",1)
                        GUICtrlCreateListViewItem($aTemp[3],$listview1)
        GUICtrlSetBkColor(-1, 0xcccccc) ; BackgrondColor even
                    Next
                    _GUICtrlListView_EndUpdate($listview1)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _SplitTime($sTime)
    ;2:34:41 PM
    Local $aTime = StringSplit($sTime, ":")
    $aTime[3] = StringReplace($aTime[3], " PM", "")
    If @extended > 0 Then $aTime[1] += 12
    Return $aTime
EndFunc   ;==>_SplitTime

and here is the Test-File i worked with: LVtoSort.Txt

mfg autoBert

Edited by AutoBert

Share this post


Link to post
Share on other sites

@AutoBert

Nice Try ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

hi autobert. thanks for the reply. great script and this works. i have the almost similar script which sorts as well.

the entry into my text log is that it appends and goes into the last line. i wanted the listview to display the last line up to the first line from the text log which i am having trouble displaying it. i thought i will be able to do that using the register_sort_callback.

i have attached the textlog.

log.txt

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  
Followers 0