how to sort List (of T)

  • Thread starter Thread starter jr
  • Start date Start date
J

jr

I created a simple class, have used it to populate a list (of myclass)
- The class only contains a customer id and a date. I would like to
sort this list based on most recent date to the oldest.
If possible please give a brief sample or a link to _VB_ code.

Thanks-

If it helps, here's the class I created:

Public Class Cust

Private _custid As String
Private _timestamp As Date


Public Property custid() As String
Get

custid = _custid

End Get
Set(ByVal value As String)

_custid = value

End Set
End Property

Public Property timestamp() As Date
Get

timestamp = _timestamp

End Get
Set(ByVal value As Date)

_timestamp = value

End Set
End Property


End Class

--
 
jr said:
I created a simple class, have used it to populate a list (of myclass)
- The class only contains a customer id and a date. I would like to
sort this list based on most recent date to the oldest.
If possible please give a brief sample or a link to _VB_ code.

Thanks-

If it helps, here's the class I created:

Public Class Cust

Private _custid As String
Private _timestamp As Date


Public Property custid() As String
Get

custid = _custid

End Get
Set(ByVal value As String)

_custid = value

End Set
End Property

Public Property timestamp() As Date
Get

timestamp = _timestamp

End Get
Set(ByVal value As Date)

_timestamp = value

End Set
End Property


End Class

Add a comparison method in the class:

Public Shared Function CompareDate(x as Cust, y as Cust) As Integer
Return DateTime.Compare(x._timestamp, y._timestamp)
End Sub

Now you can use that in the Sort method:

theList.Sort(Cust.CompareDate)
 
jr said:
Thanks, I already know how to use google. Did you bother to look at
the top 5 results? All c# except for the one MSDN which is, of course
useless.

Even adding '-C#' is useless, hence this post.

You take what you can get and you use it. No one is here to hold your hand.
 
Thanks, I already know how to use google.  Did you bother to look at
the top 5 results?  All c# except for the one MSDN which is, of course
useless.

Even adding '-C#' is useless, hence this post.





--

First off, you should be able to translate the C# to VB without much
trouble, most likely the samples aren't using C# only features. If you
want to succeed in the world of .NET it's very, very recommended to be
able to switch between VB and C# at any time. It's also not as hard as
you think to translate from one to the other, as the only difference
is syntax, the calls will still be the same.

Thanks,

Seth Rowe [MVP]
 
Back
Top