473,473 Members | 1,906 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.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
5 8786

"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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steve Wasser | last post by:
I need to sort a two-dimensional array. Each day I process a file with 9 comma-delimited fields, and varying amount of records (lines). I want to pull in each line, split it according to the comma...
9
by: Arjen | last post by:
Hello, Persons is a hashtable which I convert to an array. Person aPerson = new Person; Persons.Values.CopyTo( aPerson, 0 ); Now I can access the person items like aPerson.name or...
2
by: Mark | last post by:
Assume you have a strongly typed collection of a class called Person. The strongly typed collection implements IEnumerable so it can be databound to a server control like a DataGrid. The Person...
3
by: Alexander Widera | last post by:
Hi, I have a problem with this code ... (see below) ... I want to sort an instance of MyList ... by MyData.Shortname ... Shortname is of the type string.... how can I sort the entries? Thank...
3
by: RJN | last post by:
Hi I have a user defined data type which implements IComparable interface and has an implementation for the CompareTo method. Public Class MyDataType Implements IComparable --Private...
4
by: M Harvey | last post by:
I have an arraylist that contains datetime values. What is the best way to sort this arraylist by date ascending? Thanks, Matt
15
by: bcochofel | last post by:
Hi, I want to use a variable to sort elements. That var his passed with query string (I'm using Perl CGI to generate XML). Here's a sample of my output:...
2
by: John Devlon | last post by:
Hi, I've created my own class containing a few properties like name, zip-code and savings. At some point i'm storing several objects in an array. Does anyone know how to sort the array using...
8
by: SimeonArgus | last post by:
I need to sort a list of points, so I've considered using an IComparable implementation. Should be easy, right? But I need to know two things in the CompareTo function, not one. Test1: I need to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.