Results 1 to 4 of 4

Thread: Listview Sorter: Smarter data sorting required. Please help...

  1. #1

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Listview Sorter: Smarter data sorting required. Please help...

    Hello all,

    I am using a listview to show a bunch of data about some files. I use sorting code provided by Microsoft (http://support.microsoft.com/default...b;en-us;319399) to be able to sort on different columns of the listview. This code uses the IComparer. Sadly, the IComparer doesn't use the smarter 'Windows XP'-way of sorting.

    With XP: 2.jpg will go before 10.jpg (because the number 2 is lower than the number 10)
    With the IComparer: 10.jpg will go before 2.jpg (because the character 1 is 'lower' than character 2)

    The IComparer only sorts textwise, not by value. Is there an easy way to programmatically sort by value? (So that series of 0...9-characters will be seen as values, in stead of normal text.)

    Thanks in advance,
    Alex.
    Last edited by arsmakman; Jun 8th, 2005 at 11:46 AM.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Listview Sorter: Smarter data sorting

    If all the items in that column are numeric filenames with the ".jpg" or other extension then you could parse out the
    extension and do a numeric ICompare.

    VB Code:
    1. 'Usage:
    2. 'Me.ListView1.ListViewItemSorter = New ListViewItemComparer(ListView1.Text)
    3.  
    4. 'Implements the manual sorting of items by index.
    5. Friend Class ListViewItemComparer
    6.     Implements IComparer
    7.  
    8.     Private ind As Integer
    9.  
    10.     Public Sub New()
    11.         ind = 0
    12.     End Sub
    13.  
    14.     Public Sub New(ByVal index As Integer)
    15.         ind = index
    16.     End Sub
    17.  
    18.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
    19.         Dim sNum1 As String
    20.         Dim sNum2 As String
    21.         sNum1 = DirectCast(x, ListViewItem).Text
    22.         sNum2 = DirectCast(y, ListViewItem).Text
    23.         'ToDo: Parse out the entensions
    24.         'Compare the numeric filename without the extensions
    25.         Return DirectCast(sNum1, Integer) - DirectCast(sNum2, Integer)
    26.     End Function
    27. End Class
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: Listview Sorter: Smarter data sorting required. Please help...

    The problem is there's other columns too that need to be parsed.
    There's four columns: ID (a number to keep them linked to a row in the DataSet I use), File name (like: ##.jpg or Album - ## - Song.mp3), Folder (like: C:\Files\) and File size (format: 1.000 KB, for filesize I could you the data from my database, where size is in bytes).

    So, what I'm trying to say is that there's a lot of different types of input. So, the XP way of sorting (detecting text for numbers, then sorting on the value of those numbers it the preceding text is the same) would be best.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Listview Sorter: Smarter data sorting required. Please help...

    You could have a class for each column that is different sorting. In the column click you can set the sorting to each particular
    ListViewItemSorter class depending on which column is clicked.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width