Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > ListView.Sort Trouble


Reply
 
Thread Tools Display Modes
  #1  
Old 10-20-2006, 02:49 PM
ShadowWolf ShadowWolf is offline
Centurion
 
Join Date: Jan 2006
Location: Finland
Posts: 114
Question ListView.Sort Trouble


Hello,

I think I've managed to find a bug in the ListView control. When I tell the ListView to sort, it only sorts the first group (lv1.groups(0)) and leaves the other group unsorted. There doesn't seem to be a way to sort all groups so what do I do now?
Reply With Quote
  #2  
Old 10-20-2006, 08:13 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

Take a look at IComparer. I posted a link on a previous thread that has an example.
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
  #3  
Old 10-21-2006, 08:28 AM
ShadowWolf ShadowWolf is offline
Centurion
 
Join Date: Jan 2006
Location: Finland
Posts: 114
Default

Quote:
Originally Posted by IUnknown
Take a look at IComparer. I posted a link on a previous thread that has an example.
Ah I were hoping it could be done without that, just me missing something simple. I guess I'll make my own sorting function with IComparer then (I do not like copying). Thanks.

Edit: Manual sorting does not work on groups. The MS example does not use groups either (I wonder why ). Now, is it really impossible to sort all groups in a listview control?

Edit: Tried to put the items for the 2 groups into 2 separate listviews and sort them there before cloning the items to the visible listbox. This time there was no sorting in any of the groups. No matter how I copied the items it just wouldn't get them in the sorted order.

Last edited by ShadowWolf; 10-21-2006 at 10:00 AM.
Reply With Quote
  #4  
Old 10-21-2006, 10:56 AM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

What are groups? Can you post a short example of what you're trying to achieve.
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
  #5  
Old 10-21-2006, 12:59 PM
ShadowWolf ShadowWolf is offline
Centurion
 
Join Date: Jan 2006
Location: Finland
Posts: 114
Default

Quote:
Originally Posted by IUnknown
What are groups? Can you post a short example of what you're trying to achieve.
Code:
ListView1.Groups.Add("g1","Group 1")
ListView1.Groups.Add("g2","Group 2")

Dim LVI as ListViewItem

LVI = ListView1.Items.Add("Test 1")
LVI.Group = ListView1.Groups(0)
LVI = ListView1.Items.Add("Test 2")
LVI.Group = ListView1.Groups(1)

ListView1.Sort() 'This only sorts ListView1.Groups(0) and there is no way to set the "active" group.
Edit: What I'm trying to create is a listview with folders, sorted alphabetically at the top, and files below them, also sorted alphabetically. With groups you can separate these 2, the problem is that the files are never sorted (ListView1.Group(1)).
Reply With Quote
  #6  
Old 10-21-2006, 03:43 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

If you implement IComparer, you can sort the data in the list pretty much anyway you choose as you control what value the Compare method will return. If you want to sort within each group, you would probably need to compare both the grouping and the columns. There is an example in the MSDN2 on sorting listview groups, look for ListViewGroup Class.

Hope this helps!
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
  #7  
Old 10-22-2006, 04:58 AM
ShadowWolf ShadowWolf is offline
Centurion
 
Join Date: Jan 2006
Location: Finland
Posts: 114
Talking Solution

I read that article on MSDN2 and weren't able to get it to do anything but remove all my groups and sort folders and files mixed up in alphabetical order. However, during the night I got this idea, which is alot more simple and flexible than what M$ seems to have to offer. This is approximately what I did:

Code:
Dim DirInfo As New DirectoryInfo(Folder)
Dim Dirs() As DirectoryInfo = DirInfo.GetDirectories()
Dim Files() As FileInfo = DirInfo.GetFiles(Filter)

Array.Sort(Dirs, New DiretoryInfoComparer) 'Custom made comparer
Array.Sort(Files, New FileInfoComparer) 'Custom made comparer

For Each D As DirectoryInfo In Dirs 'Do this for the files too
'Add them to a listview with no sorting turned on
Next
One of the comparers look like this: (Still a WIP, I will add support for sorting different columns and in different orders etc.)

Code:
    Private Class DirectoryInfoComparer
        Implements IComparer

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            Dim result As Int32 = String.Compare(CType(x, DirectoryInfo).Name, CType(y, DirectoryInfo).Name)

            Return result
        End Function
    End Class
Thanks for the help IUnknown, source for some of the ideas above were from the links you gave me. I hope the code above can help other people with the same problems.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->