Jump to content

Sort a List View alphabetically when column header is clicked?


Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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