\r\n\r\n
Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Sorting System.Collections.Generic.List


\r\n \r\n
 
 
Thread Tools Display Modes

\r\n\r\n\r\n
Hello,
\n
\nI have a lists of different custom classes and each must be sorted severals times before the algorithm is complete. For each sort I wrote a comparer. Sofar so good. Everything works.
\nBut:
\nBy now I have about a dozen different comparers in more than one class and it is still getting more. I wonder if there is a way to limit the number by writing a new general function that can be used to sort all lists without the need for a specific comparer.
\n
\nBelow is what I\'ve got so far. But it does not work.
\n
\n"System.Collections.Generic.List(Of ClassA)" can not be converted into "System.Collections.Generic.List(Of Object)."
\n
\nWhat am I doing wrong?
\nAny help would be appreciated.
\n
\n
\r\n
Code:
\r\n
Public Class SortList\n\n    Private _fields() As String\n    Private _sort() As Integer\n\n    Public Sub Sort(ByRef list As List(Of Object), ByVal SortFields() As String)\n\n        ReDim _fields(SortFields.Length - 1)\n        ReDim _sort(SortFields.Length - 1)\n\n        For i As Integer = 0 To SortFields.Length - 1\n\n            Dim temp() As String = SortFields(i).Split(" ")\n\n            _fields(i) = temp(0)\n            If temp.Length > 1 Then\n                Select Case temp(1).ToLower().Trim()\n                    Case "", "asc"\n                        _sort(i) = 1\n                    Case "desc"\n                        _sort(i) = -1\n                End Select\n            Else\n                _sort(i) = 1\n            End If\n\n        Next i\n\n        Select Case SortFields.Length\n\n            Case 1\n                list.Sort(AddressOf Compare1)\n                \'Case 2\n                \'    list.Sort(AddressOf Compare2)\n                \'Case 3\n                \'    list.Sort(AddressOf Compare3)\n                \'Case 4\n                \'    list.Sort(AddressOf Compare4)\n                \'Case 5\n                \'    list.Sort(AddressOf Compare5)\n        End Select\n\n    End Sub\n\n    Private Function Compare1(x As Object, y As Object) As Integer\n\n        If x Is Nothing Then\n            If y Is Nothing Then\n                Return 0\n            Else\n                Return _sort(0) * -1\n            End If\n        Else\n            If y Is Nothing Then\n                Return _sort(0) * 1\n            Else\n                Return _sort(0) * x.GetType().GetProperty(_fields(0)).GetValue(x, Nothing).CompareTo(y.GetType().GetProperty(_fields(0)).GetValue(y, Nothing))\n            End If\n        End If\n\n    End Function\n\nEnd Class\n\nSub CalcSomething\n\n        Dim ListA As New List(Of ClassA)\n        Dim ListB As New List(Of ClassB)\n\n        Dim sl As New SortList()\n        sl.Sort(ListA, {"Field1" desc, "Field2"})\n        sl.Sort(ListB, {"FieldX desc"})\n\nEnd Sub
\r\n
\r\n \r\n\r\n
\r\n \r\n\r\n \r\n \r\n\r\n \r\n\r\n \r\n\r\n \r\n \r\n
\r\n
\r\n \r\n \r\n Last edited by Flyguy; 02-02-2012 at 03:52 AM.\r\n \r\n \r\n Reason: Added [code][/code] tags\r\n \r\n \r\n
\r\n \r\n \r\n\r\n
\r\n \r\n \r\n \r\n \r\n Reply With Quote\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n\r\n \r\n\r\n
\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n\r\n\r\n\r\n\r\n'; pd[1390393] = '\r\n\r\n \r\n\r\n
\r\n
\r\n
\r\n\r\n
\r\n \r\n\r\n\r\n\r\n\r\n\r\n \r\n\r\n\r\n\r\n \r\n
\r\n
\r\n  \r\n #2  \r\n \r\n \r\n \r\n \r\n \r\n
\r\n\r\n
\r\n \r\n Old\r\n \r\n 02-02-2012, 09:00 AM\r\n \r\n \r\n \r\n
\r\n
\r\n \r\n \r\n \r\n \r\n \r\n
AtmaWeapon\'s Avatar\r\n\r\n
\r\n \r\n AtmaWeapon\r\n AtmaWeapon is offline\r\n\r\n\r\n
Prev Previous Post   Next Post Next
  #1  
Old 02-02-2012, 03:09 AM
Blubb10 Blubb10 is offline
Newcomer
 
Join Date: Sep 2007
Posts: 9
Default Sorting System.Collections.Generic.List


Hello,

I have a lists of different custom classes and each must be sorted severals times before the algorithm is complete. For each sort I wrote a comparer. Sofar so good. Everything works.
But:
By now I have about a dozen different comparers in more than one class and it is still getting more. I wonder if there is a way to limit the number by writing a new general function that can be used to sort all lists without the need for a specific comparer.

Below is what I've got so far. But it does not work.

"System.Collections.Generic.List(Of ClassA)" can not be converted into "System.Collections.Generic.List(Of Object)."

What am I doing wrong?
Any help would be appreciated.

Code:
Public Class SortList

    Private _fields() As String
    Private _sort() As Integer

    Public Sub Sort(ByRef list As List(Of Object), ByVal SortFields() As String)

        ReDim _fields(SortFields.Length - 1)
        ReDim _sort(SortFields.Length - 1)

        For i As Integer = 0 To SortFields.Length - 1

            Dim temp() As String = SortFields(i).Split(" ")

            _fields(i) = temp(0)
            If temp.Length > 1 Then
                Select Case temp(1).ToLower().Trim()
                    Case "", "asc"
                        _sort(i) = 1
                    Case "desc"
                        _sort(i) = -1
                End Select
            Else
                _sort(i) = 1
            End If

        Next i

        Select Case SortFields.Length

            Case 1
                list.Sort(AddressOf Compare1)
                'Case 2
                '    list.Sort(AddressOf Compare2)
                'Case 3
                '    list.Sort(AddressOf Compare3)
                'Case 4
                '    list.Sort(AddressOf Compare4)
                'Case 5
                '    list.Sort(AddressOf Compare5)
        End Select

    End Sub

    Private Function Compare1(x As Object, y As Object) As Integer

        If x Is Nothing Then
            If y Is Nothing Then
                Return 0
            Else
                Return _sort(0) * -1
            End If
        Else
            If y Is Nothing Then
                Return _sort(0) * 1
            Else
                Return _sort(0) * x.GetType().GetProperty(_fields(0)).GetValue(x, Nothing).CompareTo(y.GetType().GetProperty(_fields(0)).GetValue(y, Nothing))
            End If
        End If

    End Function

End Class

Sub CalcSomething

        Dim ListA As New List(Of ClassA)
        Dim ListB As New List(Of ClassB)

        Dim sl As New SortList()
        sl.Sort(ListA, {"Field1" desc, "Field2"})
        sl.Sort(ListB, {"FieldX desc"})

End Sub

Last edited by Flyguy; 02-02-2012 at 03:52 AM. Reason: Added [code][/code] tags
Reply With Quote
 

Tags
list, sort


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