how to sort List (of T)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jr

    how to sort List (of T)

    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

    --

  • Mr. Arnold

    #2
    Re: how to sort List (of T)


    "jr" <jriggs420 (a) yahoo (d) comwrote in message
    news:OK9vhuK0IH A.1772@TK2MSFTN GP03.phx.gbl...
    If possible please give a brief sample or a link to _VB_ code.
    You should take any example and run with it.

    <http://www.google.com/search?hl=en&q= sort+List%3Ct%3 E+.Net&btnG=Goo gle+Search>

    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: how to sort List (of T)

      jr wrote:
      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.Compar e(x._timestamp, y._timestamp)
      End Sub

      Now you can use that in the Sort method:

      theList.Sort(Cu st.CompareDate)

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • jr

        #4
        Re: how to sort List (of T)

        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.



        Mr. Arnold wrote:
        >
        "jr" <jriggs420 (a) yahoo (d) comwrote in message
        news:OK9vhuK0IH A.1772@TK2MSFTN GP03.phx.gbl...
        >
        If possible please give a brief sample or a link to VB code.
        >
        You should take any example and run with it.
        >
        <http://www.google.com/search?hl=en&q...Net&btnG=Googl
        e+Search>


        --

        Comment

        • Mr. Arnold

          #5
          Re: how to sort List (of T)


          "jr" <jriggs420 (a) yahoo (d) comwrote in message
          news:%23jhNgoV0 IHA.4492@TK2MSF TNGP02.phx.gbl. ..
          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.

          Comment

          • rowe_newsgroups

            #6
            Re: how to sort List (of T)

            On Jun 18, 11:43 am, "jr" <jriggs420 (a) yahoo (d) comwrote:
            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.
            >
            Mr. Arnold wrote:
            >
            "jr" <jriggs420 (a) yahoo (d) comwrote in message
            news:OK9vhuK0IH A.1772@TK2MSFTN GP03.phx.gbl...
            >
            If possible please give a brief sample or a link to VB code.
            >
            You should take any example and run with it.
            >>
            --
            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]

            Comment

            Working...