locked
list(of T) custom sorts in VB RRS feed

  • Question

  • User-1236269922 posted

    Im converting a winforms application and it has gone smoothly except for trying to get custom sorts for generic lists working.

    VS 2010 ASP.net 4.0

    I have my classes in App_code but it fails to work like the winforms version.

    Dim ReturnList as List(of Quotedata)

    ReturnList.Sort(New SortQuoteDateAscending)

    > Unable to cast object of type 'SortQuoteDateAscending' to type 'System.Collections.Generic.IComparer`1

    Is there a gotcha with asp.net implemenation of custom sorts in asp.net 4.0 or am I missing something.

    Public Class Quotedata

       Private m_quotedate As Date
       

    Public Property QuoteDate() As Date
            Get
                Return m_quotedate
            End Get
            Set(ByVal Value As Date)
                m_quotedate = Value
            End Set

    End Property

    ....................

    Public Class SortQuoteDateAscending : Implements IComparer

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare

            Dim FirstQuote, SecondQuote As Quotedata

            Try

                FirstQuote = CType(x, Quotedata)
                SecondQuote = CType(y, Quotedata)

            Catch CompareException As SystemException
                Throw (CompareException)
            End Try

            If FirstQuote.QuoteDate = SecondQuote.QuoteDate Then
                Return 0
            ElseIf FirstQuote.QuoteDate > SecondQuote.QuoteDate Then
                Return -1
            ElseIf FirstQuote.QuoteDate < SecondQuote.QuoteDate Then
                Return 1
            Else
                Return 0
            End If

        End Function


    End Class

    Thursday, October 13, 2011 2:57 PM

Answers

  • User42903263 posted

    Another way would be to use LINQ:

    Module Module1
    
    	Class QuoteData
    		Property quoteDate As DateTime
    		Property other As String
    
    		Public Overrides Function ToString() As String
    			Return quoteDate.ToString("yyyy-MM-dd") & " " & other
    		End Function
    	End Class
    
        Sub Main()
    		Dim quotes As New List(Of QuoteData)
    		quotes.Add(New QuoteData With {.quoteDate = New Date(2011, 1, 1), .other = "some data"})
    		quotes.Add(New QuoteData With {.quoteDate = New Date(2010, 12, 31), .other = "more data"})
    
    		' ******** this is the line that does the sort *********
    		Dim dateSortedQuotes = (From q In quotes Order By q.quoteDate Select q).ToList
    
    		For Each q In dateSortedQuotes
    			Console.WriteLine(q.ToString)
    		Next
    
    		Console.ReadLine()
    
        End Sub
    
    End Module

    --
    Andrew

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2011 3:02 PM

All replies

  • User-691759321 posted

    Your best bet is to actually create a class that inherits from list.  Then create a new instance of your CustomList and add elements to that and call .Sort().

        Public Class CustomList
            Inherits List(Of CustomClass)
            Implements IComparable(Of CustomClass)
    
    
            Public Function CompareTo(ByVal other As CustomClass) As Integer Implements IComparable(Of CustomClass).CompareTo
    
            End Function
        End Class
    Thursday, October 13, 2011 3:49 PM
  • User42903263 posted

    Another way would be to use LINQ:

    Module Module1
    
    	Class QuoteData
    		Property quoteDate As DateTime
    		Property other As String
    
    		Public Overrides Function ToString() As String
    			Return quoteDate.ToString("yyyy-MM-dd") & " " & other
    		End Function
    	End Class
    
        Sub Main()
    		Dim quotes As New List(Of QuoteData)
    		quotes.Add(New QuoteData With {.quoteDate = New Date(2011, 1, 1), .other = "some data"})
    		quotes.Add(New QuoteData With {.quoteDate = New Date(2010, 12, 31), .other = "more data"})
    
    		' ******** this is the line that does the sort *********
    		Dim dateSortedQuotes = (From q In quotes Order By q.quoteDate Select q).ToList
    
    		For Each q In dateSortedQuotes
    			Console.WriteLine(q.ToString)
    		Next
    
    		Console.ReadLine()
    
        End Sub
    
    End Module

    --
    Andrew

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2011 3:02 PM
  • User-1236269922 posted

    This is probably the best solution for me right now Andrew. Can anyone explain why it doesn't work to begin with or how one could access the custom sorts in classes in the app_code directory? Winforms does, so are there rules how to use generic lists in asp.net that are different than winforms?

    Saturday, October 15, 2011 10:34 AM