Jump to content
Sign in to follow this  
Zachlr

Sort a List View alphabetically when column header is clicked?

Recommended Posts

Zachlr

Hello community,

I am looking for a way to sort a column of a List View alphabetically. I have tried using the sorting styles but they don't seem to work for whatever reason. Has anyone developed a UDF for this? Thanks.

Share this post


Link to post
Share on other sites
maqleod

Hello community,

I am looking for a way to sort a column of a List View alphabetically. I have tried using the sorting styles but they don't seem to work for whatever reason. Has anyone developed a UDF for this? Thanks.

look in the help file, under GuiListView

There are two functions

GuiCtrlListView_SimpleSort()

GuiCtrlListView_SortItems()


[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites
Zachlr

look in the help file, under GuiListView

There are two functions

GuiCtrlListView_SimpleSort()

GuiCtrlListView_SortItems()

Hmm... I wasn't able to find the GuiCtrlListView_SimpleSort() and GuiCtrlListView_SortItems() functions in the help file. Only GUICtrlCreateListView() and it didn't mention sorting items, in fact it says "Sorting the list by clicking the column name (as in Explorer) is not currently implemented." I am using version 3.3.0.0 if it makes a difference.

Share this post


Link to post
Share on other sites
maqleod

Hmm... I wasn't able to find the GuiCtrlListView_SimpleSort() and GuiCtrlListView_SortItems() functions in the help file. Only GUICtrlCreateListView() and it didn't mention sorting items, in fact it says "Sorting the list by clicking the column name (as in Explorer) is not currently implemented." I am using version 3.3.0.0 if it makes a difference.

look in the User Defined Function section


[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites
Zachlr

I'm still having trouble finding it, heh heh. Would you be able to provide the link to the online help file please? Thanks!

Share this post


Link to post
Share on other sites
maqleod

I'm still having trouble finding it, heh heh. Would you be able to provide the link to the online help file please? Thanks!

you won't find it in the online documentation, it's a part of the UDF package installed as includes, not built-in functions

you will find the documentation in the AutoIt.chm file in the AutoIt3 directory, or by pressing F1 in Scite4AutoIt3


[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites
GaryFrost

you won't find it in the online documentation, it's a part of the UDF package installed as includes, not built-in functions

you will find the documentation in the AutoIt.chm file in the AutoIt3 directory, or by pressing F1 in Scite4AutoIt3

http://dundats.mvps.org/help/


SciTE for AutoItDirections for Submitting Standard UDFs

 

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

 

Share this post


Link to post
Share on other sites
Zachlr

you won't find it in the online documentation, it's a part of the UDF package installed as includes, not built-in functions

you will find the documentation in the AutoIt.chm file in the AutoIt3 directory, or by pressing F1 in Scite4AutoIt3

Ah. I was looking in the AutoIt3.chm help file. Thanks for the information, the functions seem pretty self explanatory.

Thanks for providing the online UDF documentation, I'll be sure to bookmark it.

Share this post


Link to post
Share on other sites
Zachlr

Just something I want to throw out there, if there are too many (probably a few hundred) entries in a list view and you try to sort them using Sort_Items it will cause an AutoIT hard crash.

Share this post


Link to post
Share on other sites
martin

Just something I want to throw out there, if there are too many (probably a few hundred) entries in a list view and you try to sort them using Sort_Items it will cause an AutoIT hard crash.

Can you post some code that shows that?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Zachlr

Can you post some code that shows that?

I'd be happy to. Sorry it's kind of bulky, but it works.

Please test the script without altering it for consistency. Once you have tested it please let me know if it crashed for you, so I know if it is just my system.

#include <GUIConstants.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
;includes.  I wasn't sure which ones were required so I copied them from the original script.


; CREATE THE GUI
$GUI = GUICreate("_GUICtrlListView_SortItems causes hard crash", 800, 500)
$ListView = GUICtrlCreateListView("Item1|Item2|Item3|Item4|Item4|Item5|Item6|Item7", 0, 0, 800, 500)
GUISetState(@SW_SHOW)
$loop = 0 ;Set a loop variable to tell the script when to stop creating items
$d = "|" ; set a divider variable which kind of serves no real purpose other than saving one keypress big woop.

MsgBox(0, "Sort items crash", "Please wait until the list has finished being generated, after that, click a header to sort it.", 6)
;just some FYIs for you

_GUICtrlListView_RegisterSortCallBack($ListView)

Do
$chr0 = Chr(Random(33, 126))
$chr1 = Chr(Random(33, 126))
$chr2 = Chr(Random(33, 126))
$chr3 = Chr(Random(33, 126))
$chr4 = Chr(Random(33, 126))
$chr5 = Chr(Random(33, 126))
$chr6 = Chr(Random(33, 126))
;create random text for an entry

$text = $chr0 & $d & $chr1 & $d & $chr2 & $d & $chr3 & $d & $chr4 & $d & $chr5 & $d & $chr6 
;put it all into one var
GUICtrlCreateListViewItem($text, $ListView)
;create it
Sleep(10) ;allow some cooldown time
$loop = $loop + 1 ;add one to the loop (I hate FOR loops)
WinSetTitle("_GUICtrlListView_SortItems causes hard crash", "", "_GUICtrlListView_SortItems causes hard crash - Creating list: " & $loop & "/500")
;tell the user how many entries are left
Until $loop > 499

WinSetTitle("_GUICtrlListView_SortItems causes hard crash", "", "_GUICtrlListView_SortItems causes hard crash - Waiting for user input")
;more infoz

While 1
Switch GUIGetMsg() ;wait for input
    Case $GUI_EVENT_CLOSE
        Exit
    Case $ListView
        _GUICtrlListView_SortItems($ListView, GUICtrlGetState($ListView)) ;BOOM!
EndSwitch
WEnd

List_View_sort.au3

Edited by Zachlr

Share this post


Link to post
Share on other sites
picaxe

Quote from _GUICtrlListView_SortItems help

For use only in conjunction with _GUICtrlListView_RegisterSortCallBack

Share this post


Link to post
Share on other sites
Zachlr

Quote from _GUICtrlListView_SortItems help

Ah I forgot to put that in the example code. I did have it in the original code though, and it still crashed. I had it outside the loop so I forgot it was there, I'll update the post.

Share this post


Link to post
Share on other sites
picaxe

This works for me

#include <GUIConstants.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
;includes.  I wasn't sure which ones were required so I copied them from the original script.


; CREATE THE GUI
$GUI = GUICreate("_GUICtrlListView_SortItems causes hard crash", 800, 500)
$ListView = GUICtrlCreateListView("Item1|Item2|Item3|Item4|Item4|Item5|Item6|Item7", 0, 0, 800, 500)
GUISetState(@SW_SHOW)
_GUICtrlListView_RegisterSortCallBack($ListView)

$loop = 0 ;Set a loop variable to tell the script when to stop creating items
$d = "|" ; set a divider variable which kind of serves no real purpose other than saving one keypress big woop.

MsgBox(0, "Sort items crash", "Please wait until the list has finished being generated, after that, click a header to sort it.", 6)
;just some FYIs for you


Do
$chr0 = Chr(Random(33, 126))
$chr1 = Chr(Random(33, 126))
$chr2 = Chr(Random(33, 126))
$chr3 = Chr(Random(33, 126))
$chr4 = Chr(Random(33, 126))
$chr5 = Chr(Random(33, 126))
$chr6 = Chr(Random(33, 126))
;create random text for an entry

$text = $chr0 & $d & $chr1 & $d & $chr2 & $d & $chr3 & $d & $chr4 & $d & $chr5 & $d & $chr6
;put it all into one var
GUICtrlCreateListViewItem($text, $ListView)

;create it
Sleep(10) ;allow some cooldown time
$loop = $loop + 1 ;add one to the loop (I hate FOR loops)
WinSetTitle("_GUICtrlListView_SortItems causes hard crash", "", "_GUICtrlListView_SortItems causes hard crash - Creating list: " & $loop & "/500")
;tell the user how many entries are left
Until $loop > 499

WinSetTitle("_GUICtrlListView_SortItems causes hard crash", "", "_GUICtrlListView_SortItems causes hard crash - Waiting for user input")
;more infoz

While 1
Switch GUIGetMsg() ;wait for input
    Case $GUI_EVENT_CLOSE
        _GUICtrlListView_UnRegisterSortCallBack($ListView)
        Exit
    Case $ListView
        _GUICtrlListView_SortItems($ListView, GUICtrlGetState($ListView)) ;BOOM!
EndSwitch
WEnd
_GUICtrlListView_SortItems can be very slow when you have a lot of items (~1000 or more), search examples forum for fast listview sort if you need more speed.

Share this post


Link to post
Share on other sites
Zachlr

This works for me

#include <GUIConstants.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
;includes.  I wasn't sure which ones were required so I copied them from the original script.


; CREATE THE GUI
$GUI = GUICreate("_GUICtrlListView_SortItems causes hard crash", 800, 500)
$ListView = GUICtrlCreateListView("Item1|Item2|Item3|Item4|Item4|Item5|Item6|Item7", 0, 0, 800, 500)
GUISetState(@SW_SHOW)
_GUICtrlListView_RegisterSortCallBack($ListView)

$loop = 0 ;Set a loop variable to tell the script when to stop creating items
$d = "|" ; set a divider variable which kind of serves no real purpose other than saving one keypress big woop.

MsgBox(0, "Sort items crash", "Please wait until the list has finished being generated, after that, click a header to sort it.", 6)
;just some FYIs for you


Do
$chr0 = Chr(Random(33, 126))
$chr1 = Chr(Random(33, 126))
$chr2 = Chr(Random(33, 126))
$chr3 = Chr(Random(33, 126))
$chr4 = Chr(Random(33, 126))
$chr5 = Chr(Random(33, 126))
$chr6 = Chr(Random(33, 126))
;create random text for an entry

$text = $chr0 & $d & $chr1 & $d & $chr2 & $d & $chr3 & $d & $chr4 & $d & $chr5 & $d & $chr6
;put it all into one var
GUICtrlCreateListViewItem($text, $ListView)

;create it
Sleep(10) ;allow some cooldown time
$loop = $loop + 1 ;add one to the loop (I hate FOR loops)
WinSetTitle("_GUICtrlListView_SortItems causes hard crash", "", "_GUICtrlListView_SortItems causes hard crash - Creating list: " & $loop & "/500")
;tell the user how many entries are left
Until $loop > 499

WinSetTitle("_GUICtrlListView_SortItems causes hard crash", "", "_GUICtrlListView_SortItems causes hard crash - Waiting for user input")
;more infoz

While 1
Switch GUIGetMsg() ;wait for input
    Case $GUI_EVENT_CLOSE
        _GUICtrlListView_UnRegisterSortCallBack($ListView)
        Exit
    Case $ListView
        _GUICtrlListView_SortItems($ListView, GUICtrlGetState($ListView)) ;BOOM!
EndSwitch
WEnd
_GUICtrlListView_SortItems can be very slow when you have a lot of items (~1000 or more), search examples forum for fast listview sort if you need more speed.

Hmm... maybe it is just my system then. I was watching resource monitor when it happened, no significant change. Maybe I'm just getting bad luck. I'm not going to waste time doing a full system info write up, but I am using Windows Vista Home Premium, and AutoIt 3.3.0.0.

I'm not really worried about speed, as long as it doesn't crash. I will search the forum for an alternative though, since a different UDF might work better on my computer.

I'm interested in seeing if this happens on anyone else's computer, so let me know how it goes. I'm not sure if this enough for a bug report, but you can write one if you want.

Share this post


Link to post
Share on other sites
picaxe

Have you tried the code I posted. Your original code was missing _GUICtrlListView_RegisterSortCallBack and _GUICtrlListView_UnRegisterSortCallBack.

Share this post


Link to post
Share on other sites
rajeshontheweb
picea892

Hi

Very interested in this topic

Some nice information here. But wondering why this relatively obscure UDF that is not in the helpfile. Why not use

_GUICtrlListView_SimpleSort

Share this post


Link to post
Share on other sites
picaxe

Hi

Very interested in this topic

Some nice information here. But wondering why this relatively obscure UDF that is not in the helpfile. Why not use

_GUICtrlListView_SimpleSort

There's no column sort direction arrow when _GUICtrlListView_SimpleSort is used

Share this post


Link to post
Share on other sites
Zachlr

Have you tried the code I posted. Your original code was missing _GUICtrlListView_RegisterSortCallBack and _GUICtrlListView_UnRegisterSortCallBack.

My original code did have a _GUICtrlListView_RegisterSortCallBack function call before the GUIGetMsg() loop. It also had an Unregister that was after the loop, so it was never reached. I'm showing my noobness here, I don't know what the function counts as having an unregister. I do have one, but it is never reached. If I put it in the loop, then it will keep unregistering them. Maybe I should put a Register at the top and an Unregister at the bottom of the loop?

The fact is, the sort still works without the register and unregister calls. I don't really know what they do, but I put them in anyway.

Okay so sometimes when you tell someone the problem you are having with a math equation the answer just clicks. Well as I was writing this message, I decided to test it with and without the register calls. It turns out it ALWAYS crashes when not using register and unregister and will momentarily stop responding when sort is initiated when using register and unregister.

I could have sworn the original script was crashing with the register call though. Maybe (probably) I am going insane, I don't know. The important thing is that now, though slow, the sort is not crashing AutoIt. I just tested my example script and it also is not crashing with Register. I must have done something right when I was playing with the register calls, or I'm going insane.

Bottom line is it works now. I will look for a faster version, but thanks for all of your help everyone. I'll try to keep these stupid-a threads to a minimum (I have a lot of them :D)

Edited by Zachlr

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  

×