473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

IComparable

Hi,

Could somebody please tell me how to use Icomparable in a .NET project? Like
I was a 5 year old?

I'm trying to sort an array of objects and I can't get it to work .... How
do I modify the main class using IComparable ? How do I sort the array in
practice?

Example...

I would like to sort the array first using the zipcode, second the Lastname
and finally the age ...
Public Class Person
Private vName as String
Private vLastName as String
Private vZipcode as Integer
Private vAge as Integer

Public property Name as String
Get
Return vName
End get
Set(ByVal value As String)
vName=value
end set
end property

Public property LastName as String
Get
Return vLastName
End get
Set(ByVal value As String)
vLastName=value
end set
end property

Public property Zipcode as Integer
Get
Return vZipcode
End get
Set(ByVal value As Integer)
vZipcode=value
end set
end property

Public property Age as Integer
Get
Return vAge
End get
Set(ByVal value As Integer)
vAge=value
end set
end property

End Class

Many, many thanks

John

Mar 17 '07 #1
2 1744
John,

Your Person class would implement the generic version of IComparable like
this:

Public Class Person
Implements IComparable(Of Person)

Your Person class then needs to implement IComparable's CompareTo method:

Public Function CompareTo(ByVal person As Person) As Integer Implements
System.IComparable(Of Person).CompareTo

If Me.Zipcode = person.Zipcode Then
If Me.LastName = person.LastName Then
Return Me.Age.CompareTo(person.Age)
Else
Return Me.LastName.CompareTo(person.LastName)
End If
Else
Return Me.Zipcode.CompareTo(person.Zipcode)
End If

End Function

Now all you need to do is add objects of type Person to an array (or an
arraylist or a List (Of Person)) and then call the array's Sort method.

Kerry Moorman
"John Devlon" wrote:
Hi,

Could somebody please tell me how to use Icomparable in a .NET project? Like
I was a 5 year old?

I'm trying to sort an array of objects and I can't get it to work .... How
do I modify the main class using IComparable ? How do I sort the array in
practice?

Example...

I would like to sort the array first using the zipcode, second the Lastname
and finally the age ...
Public Class Person
Private vName as String
Private vLastName as String
Private vZipcode as Integer
Private vAge as Integer

Public property Name as String
Get
Return vName
End get
Set(ByVal value As String)
vName=value
end set
end property

Public property LastName as String
Get
Return vLastName
End get
Set(ByVal value As String)
vLastName=value
end set
end property

Public property Zipcode as Integer
Get
Return vZipcode
End get
Set(ByVal value As Integer)
vZipcode=value
end set
end property

Public property Age as Integer
Get
Return vAge
End get
Set(ByVal value As Integer)
vAge=value
end set
end property

End Class

Many, many thanks

John

Mar 18 '07 #2
Hi Kerry,

Many Many Thanks... Your the best ... you saved my life ...

How can I repay you ?

John
Mar 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon | last post by:
Hi all I have a class implements IComparable interface, however, I got a error on the following method protected override int IComparable.CompareTo(object value .. Does anyone know the...
3
by: Cybertof | last post by:
Hello, Could someone please explain me the difference between the 2 interfaces IComparable and IComparer ? In which cases use one or the other ? Are they dedicated to own classes or built-in...
1
by: raffelm | last post by:
In the first class below, I get an error that AnotherClientBaseDataRecord does not implement IComparable.CompareTo(object)...yet the second class below is implemented identicially and it does get...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
2
by: Stefan L | last post by:
Hi everybody, when migrating to the .NET 2.0 framework we decided to encourage the use of generic functions because they ought to run faster than their equivalent object-implemantations. Now...
8
by: fniles | last post by:
I have a collection inside a class, sometimes when I add to the collection, I get the error "At least one object must implement IComparable". What does the error mean ? Thanks. Public Class...
1
by: Brett Romero | last post by:
What are the reasons to use one over the other? SortedList implements IComparer.Compare(). Why would you also want to create a SortedList class that implements IComparable.CompareTo()? I know...
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...
3
by: Samuel R. Neff | last post by:
You can you program against generic interfaces generically? For example, how can I make the following code which works for the non-generic interface also work for the generic counterparts? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.