ListView - Is it possible to sort only specified columns? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

ListView - Is it possible to sort only specified columns?

28 Jun 2017, 14:21

ListView - Is it possible to sort only specified columns?

For example, a listview contains 10 columns,

LV_ModifyCol(6, "Col4 Col5 SortDesc")

from the code above, the "column 6" shall be DescSorted and "column4/column5" shall be sorted accordingly, but, the others columns shall not be sorted and be kept unchanged.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: ListView - Is it possible to sort only specified columns?

28 Jun 2017, 19:31

"This function will apply sorting arrows to your ListView column headers"

I didn't mention no arrows, the function didn't help, but thanks anyway.

What I basically wanted to say with the example above is, when "column6" is sorted, only "column4" and "column5" should be sorted along with it, and the other columns should be left unchanged!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: ListView - Is it possible to sort only specified columns?

28 Jun 2017, 19:56

Could you cheat and use 3 listviews?

Otherwise I suppose you would have to capture the clicks onto the column headers, (possibly via OnMessage, or setting the WndProc for the listview control, or maybe AHK can notify you of such clicks via the Gui command,) record the text for the listview before the sort, perhaps via ControlGet, apply the sort to the whole listview, and then restore the text for columns 1 to 3 and 7+.

[EDIT:] Would 1 to 3, and 7 to 10 remain static? Possibly you could have a hidden column 11, containing the original order for those columns. Or you don't even have to retrieve the content for any columns, because you already know what they are at the beginning. Do you have any information to give about the nature of the columns?
Last edited by jeeswg on 28 Jun 2017, 20:15, edited 3 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: ListView - Is it possible to sort only specified columns?

28 Jun 2017, 20:10

jeeswg wrote:.
It's a possibility for sure, but I want something more straightforward.
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: ListView - Is it possible to sort only specified columns?

29 Jun 2017, 01:48

ListViews contain rows (items) horizontally split into columns (subitems) in report view. You can access their contents only by row/item number. If you change the vertical order of one/some column(s) independently of the others you change the contents of the rows. It doesn't make sense for me (even though Excel had (has?) such an annoying 'feature').
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: ListView - Is it possible to sort only specified columns?

29 Jun 2017, 01:53

just me wrote:ListViews contain rows (items) horizontally split into columns (subitems) in report view. You can access their contents only by row/item number. If you change the vertical order of one/some column(s) independently of the others you change the contents of the rows. It doesn't make sense for me (even though Excel had (has?) such an annoying 'feature').
+1 :thumbup:
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: ListView - Is it possible to sort only specified columns?  Topic is solved

29 Jun 2017, 23:43

jeeswg wrote:Do you have any information to give about the nature of the columns?
@jeeswg, I wanted to random sort only the specified columns from a listview control,

so I decided to write "ColRandomSort( )" function!

ColRandomSort("1") _ random sorts only column 1
ColRandomSort("2,4") _ random sorts only column 2 and 4
ColRandomSort("13,7,1") _ random sorts only column 13, 7 and 1
and so on...

The function may need more tests, but so far it works with no problems!

Code: Select all


loop, 15
ListCols .= "Test " a_index "|"

gui, add, listview, w700 h250, % ListCols

loop, 10
LV_Add(, a_index "_ " Chr(a_index + 64), a_index "_ " Chr(a_index + 74), a_index "_ " Chr(a_index + 84), a_index "_ " Chr(a_index + 94), a_index "_ " Chr(a_index + 104), a_index "_ " Chr(a_index + 114), a_index "_ " Chr(a_index + 124),  a_index "_ " Chr(a_index + 134), a_index "_ " Chr(a_index + 144), a_index "_ " Chr(a_index + 154), a_index "_ " Chr(a_index + 164), a_index "_ " Chr(a_index + 174), a_index "_ " Chr(a_index + 184), a_index "_ " Chr(a_index + 194), a_index "_ " Chr(a_index + 204))

gui, add, button, gSort, Sort 1
gui, add, button, x+5 gSort, Sort 1,2
gui, add, button, x+5 gSort, Sort 1,3
gui, add, button, x+5 gSort, Sort 1,4
gui, add, button, x+5 gSort, Sort 1,2,3,4
gui, add, button, x+5 gSort, Sort 15,1
gui, add, button, x+5 gSort, Sort 14,15
gui, add, button, x+5 gSort, Sort 15

gui, add, button, xm gSort, Sort 2,3
gui, add, button, x+5 gSort, Sort 12
gui, add, button, x+5 gSort, Sort 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

gui, show
return

Sort:	;___________ Sort ____________

if (a_guicontrol = "Sort 1")
ColRandomSort("1")

if (a_guicontrol = "Sort 1,2")
ColRandomSort("1,2")

else if (a_guicontrol = "Sort 1,3")
ColRandomSort("1,3")

else if (a_guicontrol = "Sort 1,4")
ColRandomSort("1,4")

else if (a_guicontrol = "Sort 15,1")
ColRandomSort("15,1")

else if (a_guicontrol = "Sort 14,15")
ColRandomSort("14,15")

else if (a_guicontrol = "Sort 15")
ColRandomSort("15")

else if (a_guicontrol = "Sort 1,2,3,4")
ColRandomSort("1,2,3,4")

else if (a_guicontrol = "Sort 2,3")
ColRandomSort("2,3")

else if (a_guicontrol = "Sort 12")
ColRandomSort("12")

else if (a_guicontrol = "Sort 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15")
ColRandomSort("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15")

return

guiclose:	;__________ Gui close ____________
exitapp


ColRandomSort(Cols)	;______________ ColRandomSort(Function) ________________
{
TotalRows := LV_GetCount( )	;"LV_GetCount( )",  returns the total number of rows in the listview control

	loop, % TotalRows
	{
	Random, Rdm1, 1, TotalRows	;"1" is the smallest number and  "TotalRows" is the largest number that can be generated
	Random, Rdm2, 1, TotalRows	;"Rdm1" and "Rdm2", the variables where to store the random generated numbers

	StartPos := 1	;necessary, otherwise, the function will not work as intended
	
		while, StartPos := RegExMatch(Cols,"\d+", Match, StartPos + StrLen(Match))
		{
		LV_GetText(Text1, Rdm1, Match)		;"Match" is col, "Rdm1" is row,  store in "Text1" variable
		LV_GetText(Text2, Rdm2, Match)		;"Match" is col, "Rdm2" is row,  store in "Text2" variable

		LV_Modify(Rdm1, "col" Match, Text2)	;set "Text2" value in "Rdm1" row from "Match" col
		LV_Modify(Rdm2, "col" Match, Text1)	;set "Text1" value in "Rdm2" row from "Match" col
		}
	}
}






Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mikeyww, OrangeCat, ShatterCoder and 85 guests