By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,722 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,722 IT Pros & Developers. It's quick & easy.

Sort descending using IComparable ?

P: n/a
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.IComparable(Of clsPerson).CompareTo
If Me.Language = myPerson.Language Then
If Me.City = myPerson.City Then
Return Me.Name.CompareTo(myPerson.Name)
Else
Return Me.City.CompareTo(myPerson.City)
End If
Else
Return Me.Language.CompareTo(myPerson.Language)
End If

End Function
Jun 11 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"John Devlon" <jo********@hotmail.comwrote in message
news:mJ*******************@phobos.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.

http://samples.gotdotnet.com/quickst.../doc/sort.aspx

The link talks about Key/Item pairs sorting.

http://msdn2.microsoft.com/en-us/lib...rt(vs.80).aspx

Jun 11 '07 #2

P: n/a
Hello,

Just return the opposite sign of the comparison.

Public Function CompareTo(ByVal myPerson As clsPerson) As Integer
Implements System.IComparable(Of clsPerson).CompareTo
Dim result As Integer

If Me.Language = myPerson.Language Then
If Me.City = myPerson.City Then
result = Me.Name.CompareTo(myPerson.Name)
Else
result = Me.City.CompareTo(myPerson.City)
End If
Else
result = Me.Language.CompareTo(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.IComparable(Of clsPerson).CompareTo
If Me.Language = myPerson.Language Then
If Me.City = myPerson.City Then
Return Me.Name.CompareTo(myPerson.Name)
Else
Return Me.City.CompareTo(myPerson.City)
End If
Else
Return Me.Language.CompareTo(myPerson.Language)
End If

End Function

Jun 11 '07 #3

P: n/a
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.IComparable(Of clsPerson).CompareTo
If Me.Language = myPerson.Language Then
If Me.City = myPerson.City Then
Return -Me.Name.CompareTo(myPerson.Name) ' return opposite
Else
Return Me.City.CompareTo(myPerson.City)
End If
Else
Return Me.Language.CompareTo(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.IComparable(Of clsPerson).CompareTo
If Me.Language = myPerson.Language Then
If Me.City = myPerson.City Then
Return Me.Name.CompareTo(myPerson.Name)
Else
Return Me.City.CompareTo(myPerson.City)
End If
Else
Return Me.Language.CompareTo(myPerson.Language)
End If

End Function

Jun 11 '07 #4

P: n/a

Dear Ms Ethridge,

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

Kind regards,

John


"Kelly Ethridge" <ke***@kellyethridge.comwrote in message
news:ui**************@TK2MSFTNGP04.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.IComparable(Of clsPerson).CompareTo
If Me.Language = myPerson.Language Then
If Me.City = myPerson.City Then
Return -Me.Name.CompareTo(myPerson.Name) ' return opposite
Else
Return Me.City.CompareTo(myPerson.City)
End If
Else
Return Me.Language.CompareTo(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.IComparable(Of clsPerson).CompareTo
If Me.Language = myPerson.Language Then
If Me.City = myPerson.City Then
Return Me.Name.CompareTo(myPerson.Name)
Else
Return Me.City.CompareTo(myPerson.City)
End If
Else
Return Me.Language.CompareTo(myPerson.Language)
End If

End Function

Jun 11 '07 #5

P: n/a
Dear Mr. Arnold,

Many thanx for the usefull links...

Kind regards

John
"Mr. Arnold" <MR. Ar****@Arnold.comwrote in message
news:ek*************@TK2MSFTNGP02.phx.gbl...
>
"John Devlon" <jo********@hotmail.comwrote in message
news:mJ*******************@phobos.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.

http://samples.gotdotnet.com/quickst.../doc/sort.aspx

The link talks about Key/Item pairs sorting.

http://msdn2.microsoft.com/en-us/lib...rt(vs.80).aspx

Jun 11 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.