Sort descending using IComparable ?

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

    Sort descending using IComparable ?

    Hi everyone,

    With this post I would like to ask you all a small question.

    I'm trying to sort an array of objects using IComparable. Adding below
    displayed code to a class and triggering the sort function works fine. Thats
    not the problem.

    The only thing I would like to change is: I would like sort the Name
    descending (starting with the z)...
    Does anyone know how ?

    Many Thanx

    John

    Ps: using VB.NET 2005


    Code:

    Public Function CompareTo(ByVal myPerson As clsPerson) As Integer Implements
    System.ICompara ble(Of clsPerson).Comp areTo
    If Me.Language = myPerson.Langua ge Then
    If Me.City = myPerson.City Then
    Return Me.Name.Compare To(myPerson.Nam e)
    Else
    Return Me.City.Compare To(myPerson.Cit y)
    End If
    Else
    Return Me.Language.Com pareTo(myPerson .Language)
    End If

    End Function


  • Mr. Arnold

    #2
    Re: Sort descending using IComparable ?


    "John Devlon" <johndevlon@hot mail.comwrote in message
    news:mJ7bi.6705 $QZ3.808180@pho bos.telenet-ops.be...
    Hi everyone,
    >
    With this post I would like to ask you all a small question.
    >
    I'm trying to sort an array of objects using IComparable. Adding below
    displayed code to a class and triggering the sort function works fine.
    Thats not the problem.
    >
    The only thing I would like to change is: I would like sort the Name
    descending (starting with the z)...
    Does anyone know how ?
    >
    >
    The link talks about Array.Sort and then Array.Reverse.



    The link talks about Key/Item pairs sorting.



    Comment

    • Kelly Ethridge

      #3
      Re: Sort descending using IComparable ?

      Hello,

      Just return the opposite sign of the comparison.

      Public Function CompareTo(ByVal myPerson As clsPerson) As Integer
      Implements System.ICompara ble(Of clsPerson).Comp areTo
      Dim result As Integer

      If Me.Language = myPerson.Langua ge Then
      If Me.City = myPerson.City Then
      result = Me.Name.Compare To(myPerson.Nam e)
      Else
      result = Me.City.Compare To(myPerson.Cit y)
      End If
      Else
      result = Me.Language.Com pareTo(myPerson .Language)
      End If

      Return -result ' return opposite
      End Function

      John Devlon wrote:
      Hi everyone,
      >
      With this post I would like to ask you all a small question.
      >
      I'm trying to sort an array of objects using IComparable. Adding below
      displayed code to a class and triggering the sort function works fine. Thats
      not the problem.
      >
      The only thing I would like to change is: I would like sort the Name
      descending (starting with the z)...
      Does anyone know how ?
      >
      Many Thanx
      >
      John
      >
      Ps: using VB.NET 2005
      >
      >
      Code:
      >
      Public Function CompareTo(ByVal myPerson As clsPerson) As Integer Implements
      System.ICompara ble(Of clsPerson).Comp areTo
      If Me.Language = myPerson.Langua ge Then
      If Me.City = myPerson.City Then
      Return Me.Name.Compare To(myPerson.Nam e)
      Else
      Return Me.City.Compare To(myPerson.Cit y)
      End If
      Else
      Return Me.Language.Com pareTo(myPerson .Language)
      End If
      >
      End Function
      >
      >

      Comment

      • Kelly Ethridge

        #4
        Re: Sort descending using IComparable ?

        Hello again,

        I replied to quickly without realizing you only wanted to change the
        sorting order of the Name property. Just return the opposite for the
        Name comparison only.

        Public Function CompareTo(ByVal myPerson As clsPerson) As Integer
        Implements System.ICompara ble(Of clsPerson).Comp areTo
        If Me.Language = myPerson.Langua ge Then
        If Me.City = myPerson.City Then
        Return -Me.Name.Compare To(myPerson.Nam e) ' return opposite
        Else
        Return Me.City.Compare To(myPerson.Cit y)
        End If
        Else
        Return Me.Language.Com pareTo(myPerson .Language)
        End If
        End Function


        John Devlon wrote:
        Hi everyone,
        >
        With this post I would like to ask you all a small question.
        >
        I'm trying to sort an array of objects using IComparable. Adding below
        displayed code to a class and triggering the sort function works fine. Thats
        not the problem.
        >
        The only thing I would like to change is: I would like sort the Name
        descending (starting with the z)...
        Does anyone know how ?
        >
        Many Thanx
        >
        John
        >
        Ps: using VB.NET 2005
        >
        >
        Code:
        >
        Public Function CompareTo(ByVal myPerson As clsPerson) As Integer Implements
        System.ICompara ble(Of clsPerson).Comp areTo
        If Me.Language = myPerson.Langua ge Then
        If Me.City = myPerson.City Then
        Return Me.Name.Compare To(myPerson.Nam e)
        Else
        Return Me.City.Compare To(myPerson.Cit y)
        End If
        Else
        Return Me.Language.Com pareTo(myPerson .Language)
        End If
        >
        End Function
        >
        >

        Comment

        • John Devlon

          #5
          Re: Sort descending using IComparable ?


          Dear Ms Ethridge,

          Your absolutely amazing. It works perfect. Your the best.
          How can I ever repay you ?

          Kind regards,

          John




          "Kelly Ethridge" <kelly@kellyeth ridge.comwrote in message
          news:uiP$xDCrHH A.1200@TK2MSFTN GP04.phx.gbl...
          Hello again,
          >
          I replied to quickly without realizing you only wanted to change the
          sorting order of the Name property. Just return the opposite for the Name
          comparison only.
          >
          Public Function CompareTo(ByVal myPerson As clsPerson) As Integer
          Implements System.ICompara ble(Of clsPerson).Comp areTo
          If Me.Language = myPerson.Langua ge Then
          If Me.City = myPerson.City Then
          Return -Me.Name.Compare To(myPerson.Nam e) ' return opposite
          Else
          Return Me.City.Compare To(myPerson.Cit y)
          End If
          Else
          Return Me.Language.Com pareTo(myPerson .Language)
          End If
          End Function
          >
          >
          John Devlon wrote:
          >Hi everyone,
          >>
          >With this post I would like to ask you all a small question.
          >>
          >I'm trying to sort an array of objects using IComparable. Adding below
          >displayed code to a class and triggering the sort function works fine.
          >Thats not the problem.
          >>
          >The only thing I would like to change is: I would like sort the Name
          >descending (starting with the z)...
          >Does anyone know how ?
          >>
          >Many Thanx
          >>
          >John
          >>
          >Ps: using VB.NET 2005
          >>
          >>
          >Code:
          >>
          >Public Function CompareTo(ByVal myPerson As clsPerson) As Integer
          >Implements System.ICompara ble(Of clsPerson).Comp areTo
          > If Me.Language = myPerson.Langua ge Then
          > If Me.City = myPerson.City Then
          > Return Me.Name.Compare To(myPerson.Nam e)
          > Else
          > Return Me.City.Compare To(myPerson.Cit y)
          > End If
          > Else
          > Return Me.Language.Com pareTo(myPerson .Language)
          > End If
          >>
          >End Function

          Comment

          • John Devlon

            #6
            Re: Sort descending using IComparable ?

            Dear Mr. Arnold,

            Many thanx for the usefull links...

            Kind regards

            John


            "Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
            news:ekpjgVBrHH A.192@TK2MSFTNG P02.phx.gbl...
            >
            "John Devlon" <johndevlon@hot mail.comwrote in message
            news:mJ7bi.6705 $QZ3.808180@pho bos.telenet-ops.be...
            >Hi everyone,
            >>
            >With this post I would like to ask you all a small question.
            >>
            >I'm trying to sort an array of objects using IComparable. Adding below
            >displayed code to a class and triggering the sort function works fine.
            >Thats not the problem.
            >>
            >The only thing I would like to change is: I would like sort the Name
            >descending (starting with the z)...
            >Does anyone know how ?
            >>
            >>
            >
            The link talks about Array.Sort and then Array.Reverse.
            >

            >
            The link talks about Key/Item pairs sorting.
            >
            http://msdn2.microsoft.com/en-us/lib...rt(vs.80).aspx

            Comment

            Working...