Jump to content



Photo

How to arrange Listview items from latest to oldest


  • Please log in to reply
6 replies to this topic

#1 JohnRichard

JohnRichard

    Prodigy

  • Active Members
  • PipPipPip
  • 196 posts

Posted 06 April 2011 - 06:19 AM

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







#2 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,496 posts

Posted 06 April 2011 - 06:47 AM

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

  AutoIt Version : 3.3.8.1/3.3.9.4 SciTE 3.3.0 Language:040C OS:WIN_7/ CPU:X64 OS:X64 

  Last updated Scripts and executables with full embedded files are available on : GoogleCode 


#3 JohnRichard

JohnRichard

    Prodigy

  • Active Members
  • PipPipPip
  • 196 posts

Posted 06 April 2011 - 07:01 AM

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

#4 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 7,068 posts

Posted 06 April 2011 - 11:59 PM

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.

How to ask questions the smart way!

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#5 AutoBert

AutoBert

    Wayfarer

  • Active Members
  • Pip
  • 98 posts

Posted 07 April 2011 - 03:03 AM

Hello JohnRichard,

here's a little try to solve or problem:
AutoIt         
#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: Attached File  LVtoSort.Txt   366bytes   79 downloads

mfg autoBert

Edited by AutoBert, 07 April 2011 - 03:12 AM.


#6 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,496 posts

Posted 07 April 2011 - 06:18 AM

@AutoBert

Nice Try ! Posted Image

  AutoIt Version : 3.3.8.1/3.3.9.4 SciTE 3.3.0 Language:040C OS:WIN_7/ CPU:X64 OS:X64 

  Last updated Scripts and executables with full embedded files are available on : GoogleCode 


#7 JohnRichard

JohnRichard

    Prodigy

  • Active Members
  • PipPipPip
  • 196 posts

Posted 07 April 2011 - 08:26 AM

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.

Attached Files

  • Attached File  log.txt   495bytes   68 downloads





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users