ListView Sorting

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
elation
Posts: 21
Joined: Thu Jul 06, 2017 12:17 pm

ListView Sorting

Post by elation »

Morning. I've been having an odd problem with listview sorting. If $listview.sorting is set to 'Ascending' or 'Descending' and then to 'None', it will default to 'Descending' when using the Add-ListViewItem -Group function. I've tried many different approaches and the data is always properly ordered before being added to the listview. Once it is added, each group will show as descending.

Example:

Code: Select all

Add-ListViewItem -ListView $listview1 -Items 'Test13' -Group Group1
ListViewTesting.psf
ListView_Sorting_Example
(19.08 KiB) Downloaded 12 times

User avatar
elation
Posts: 21
Joined: Thu Jul 06, 2017 12:17 pm

Re: ListView Sorting

Post by elation »

@jvierra Saw a reply from you but the post didn't seem to make it.

jvierra
Posts: 14459
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: ListView Sorting

Post by jvierra »

I had to look it over some more. There was an explanation for this posted a number of years ago. I couldn't find it so I am trying to reconstruct it. Give me a bit as I am in the middle of something else.

User avatar
elation
Posts: 21
Joined: Thu Jul 06, 2017 12:17 pm

Re: ListView Sorting

Post by elation »

No rush at all. Many thanks.

jvierra
Posts: 14459
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: ListView Sorting

Post by jvierra »

Now that I can focus, I think I can give a simple explanation of what is happening.

We can build a ListView and add items and groups easily. Sorting is an after the fact method to adding items.

The following from the docs gives the rules of the sorting:
Typically items are sorted using the Sorting property, which sorts items based on the item text. To customize the sort order, you must write a class that implements the IComparer interface and set the ListViewItemSorter property to an object of that class. This is useful, for example, when you want to sort items by subitem text. For more information on performing manual sorting of items, see the example for the ListViewItemSorter property.
If the ListView.Sorting property is set to a value other than SortOrder.None or if the ListViewItemSorter property is set, the list is sorted automatically when items are added. Items are not sorted automatically when the label text changes.
If you set sorting before adding the items will be sorted as they are added. If you don’t they will be displayed in the order added.

Changing the sort order will resort the items. Changing the “Sorting” property to “None” will leave the items in the current order. It will not return them to the original order as added.
This is a common misunderstanding of what sorting does when sorting unkeyed items. In SQL specifying an “ORDER BY” does not change the order of the data in storage. With controls and in-place sorts the original order is lost after the first sort. This is by design. It would take a special implementation for a ListView to behave differently.

I hope this helps you understand a commonly misunderstood issue. I blame Microsoft for this ambiguity. It would make more sense to have two properties called “Sort” and “SortOrder”. This would make this more explicit. Instead MS chose to overload the meaning into “Sorting”.

Programmers and scripters also need to understand that in place assorted data cannot be “unsorted”. There is no way to know what the original order was. SQL databases do not sort the data – they retrieve the data according to a sorted index that is either declared or dynamically created. The data is never changed.

I had no demo of this available in my pile of hundreds of examples so I built a simple example that you can play with to gain a better understanding of what is happening.
Attachments
ListViewTesting.psf
(38.71 KiB) Downloaded 12 times

User avatar
elation
Posts: 21
Joined: Thu Jul 06, 2017 12:17 pm

Re: ListView Sorting

Post by elation »

Fantastic explanation to exactly what is happening to me. I am going to look over the MS docs you mentioned. I'm determined to find a way to get the desired results. Might take some rethinking or a different approach but where there's a will there's a way. Thanks for the example and detailed explanation!

jvierra
Posts: 14459
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: ListView Sorting

Post by jvierra »

You have failed to be explicit as to the exact result you want. There are ways to accomplish sorts and even sorsts that can be reversed. "Sorting" cannot do thiat by "Sort" can with some help.

User avatar
elation
Posts: 21
Joined: Thu Jul 06, 2017 12:17 pm

Re: ListView Sorting

Post by elation »

In a nutshell, I want these 3 options for list views. Each one is a new data pull with the 3rd having defined groups.
Ascending (list)
Descending (list)
Original/No sorting (grouped)

With your answer of why it's doing what it's doing, I now have a clear path to a solution. I'll post back when I nail it down. <big thumbs up>

User avatar
elation
Posts: 21
Joined: Thu Jul 06, 2017 12:17 pm

Re: ListView Sorting

Post by elation »

So basically sorting on anything grouped is what messed it all up. Simply keeping sorts to 'List' items only (not 'SmallIcon') works as expected. It's seems a bit of an odd implementation how it works but I'll take it. I spent hours yesterday and no luck. With you advice, I found the problem in a couple minutes. Thank you very much!

Example:

Code: Select all

function Set-Ascending
{
	$listview1.View = 'List'	
	$listview1.Sorting = 'Ascending'
}
function Set-Descending
{
	$listview1.View = 'List'	
	$listview1.Sorting = 'Descending'
}
function Set-Grouped
{
	$listview1.View = 'SmallIcon'
	#No Sorting
}

Locked