473,657 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Icomparer Interface

hi,

I am trying to arrange a CollectionOfCar s in increasing order of their
X-Cordinate location.
CollectionOfCar s is a generic collection.
Dim CollectionOfCar s as new list(of Car)

Here is the class Car in the simplest form

Class CarPosition
dim _location() as double
public readonly property location() as double
return _location
end property
End class
This is how i tried to sort them... but gives error

CollectionOfCar s.sort(new CarComparer)

Class Car Comparer
implemnts Icomparer(of Car)
'In the above line, it gives error
"Class must implement compare function but i have already implemnted
End Class

Can someone give some ideas please
Mar 20 '06 #1
4 1407
Irfan <ir***@asc-ltd.co.uk> wrote:
I am trying to arrange a CollectionOfCar s in increasing order of their
X-Cordinate location.
CollectionOfCar s is a generic collection.
Dim CollectionOfCar s as new list(of Car)

Here is the class Car in the simplest form

Class CarPosition
dim _location() as double
public readonly property location() as double
return _location
end property
End class
This is how i tried to sort them... but gives error

CollectionOfCar s.sort(new CarComparer)

Class Car Comparer
implemnts Icomparer(of Car)
'In the above line, it gives error
"Class must implement compare function but i have already implemnted
End Class

Can someone give some ideas please


Well, you haven't really given enough code to show what's wrong, and
the code you *have* given isn't your real code (your class is
"CarCompare r" not "Car Comparer".

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 20 '06 #2
Thanks for the suggestions which helped me to go one step ahead. The sort
works fine now however here is my second problem.
The code is full now and can be copied to a compiler.

The code below sorts a collection of cars in increasing
order of X-cordinate. But if two cars have same Xcordinate i want then to
be arranged in increasing order of Y.
The code below sorts the CarsCollection as (Merc, Toyoto, Jag, Volvo)
however i want them (Toyoto,Merc , Jag, Volvo)
because Toyoto has YCord less than Merc although both have same X-cord.
Public Class car
Dim loc As Double()
Dim carName As String
Sub New(ByVal _name As String, ByVal _loc As Double())
loc = _loc
carName = _name
End Sub
Public ReadOnly Property Location() As Double()
Get
Return loc
End Get
End Property
'Other properties here
Shared Sub main()
Dim ocar As New car("Jag", New Double() {10, 10})
Dim ocar1 As New car("Volvo", New Double() {11, 10})
Dim ocar2 As New car("Merc", New Double() {5, 11})
Dim ocar3 As New car("Toyoto", New Double() {5, 10})

Dim CarCollection As New List(Of car)
CarCollection.A dd(ocar)
CarCollection.A dd(ocar1)
CarCollection.A dd(ocar2)
CarCollection.A dd(ocar3)
CarCollection.S ort(New CarComparer)
End Sub

Public Class CarComparer
Implements IComparer(Of car)
Public Function Compare(ByVal car1 As car, ByVal car2 As car) As Integer
Implements IComparer(Of WindowsApplicat ion4.car).Compa re
Dim car1Location As Double() = car1.Location
Dim Car2Location As Double() = car2.Location
Return car1.Location(0 ) - car2.Location(0 )
End Function

End Class

End Class

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Irfan <ir***@asc-ltd.co.uk> wrote:
I am trying to arrange a CollectionOfCar s in increasing order of their
X-Cordinate location.
CollectionOfCar s is a generic collection.
Dim CollectionOfCar s as new list(of Car)

Here is the class Car in the simplest form

Class CarPosition
dim _location() as double
public readonly property location() as double
return _location
end property
End class
This is how i tried to sort them... but gives error

CollectionOfCar s.sort(new CarComparer)

Class Car Comparer
implemnts Icomparer(of Car)
'In the above line, it gives error
"Class must implement compare function but i have already implemnted
End Class

Can someone give some ideas please


Well, you haven't really given enough code to show what's wrong, and
the code you *have* given isn't your real code (your class is
"CarCompare r" not "Car Comparer".

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 21 '06 #3
Irfan <ir***@asc-ltd.co.uk> wrote:
Thanks for the suggestions which helped me to go one step ahead. The sort
works fine now however here is my second problem.
The code is full now and can be copied to a compiler.

The code below sorts a collection of cars in increasing
order of X-cordinate. But if two cars have same Xcordinate i want then to
be arranged in increasing order of Y.
The code below sorts the CarsCollection as (Merc, Toyoto, Jag, Volvo)
however i want them (Toyoto,Merc , Jag, Volvo)
because Toyoto has YCord less than Merc although both have same X-cord.


Right. In other words, your Compare method needs to look at
Location(0) values, and return the value it's returning at the moment
if they're different - but if they're both the same, return the
difference between the two Location(1) values.

Does that give you enough of a hint?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 21 '06 #4
thanks Skeet, this is what i have exactly done after i posted the question
to you.

Thanks again
Irfan

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Irfan <ir***@asc-ltd.co.uk> wrote:
Thanks for the suggestions which helped me to go one step ahead. The sort
works fine now however here is my second problem.
The code is full now and can be copied to a compiler.

The code below sorts a collection of cars in increasing
order of X-cordinate. But if two cars have same Xcordinate i want then
to
be arranged in increasing order of Y.
The code below sorts the CarsCollection as (Merc, Toyoto, Jag, Volvo)
however i want them (Toyoto,Merc , Jag, Volvo)
because Toyoto has YCord less than Merc although both have same X-cord.


Right. In other words, your Compare method needs to look at
Location(0) values, and return the value it's returning at the moment
if they're different - but if they're both the same, return the
difference between the two Location(1) values.

Does that give you enough of a hint?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 22 '06 #5

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

Similar topics

8
2482
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a - b. But for float the results would have to be normalize some how to a 32bit range. I understand there would be percision errors. Thanks Tom
3
30170
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 collections ? Regards,
8
305
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a - b. But for float the results would have to be normalize some how to a 32bit range. I understand there would be percision errors. Thanks Tom
3
2370
by: jtfaulk | last post by:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional I have a multi-dimensional arraylist, and I would like to sort one level of it but not all. The multi-dimensional arraylist represents my menu system, and I would like to sort the third level menu items only and not the first two. So, I've been trying to use some of the suggestions I've found here about sorting. Mainly, I've created a comparison interface.
10
2529
by: Tony | last post by:
Hello! I'm reading in a book and this can't be correct it says. "Objects passed to Comparer.Compare() are checked to see if they support IComparable. If they do, then that implementation is used" Assume I have a collection of Item(s) in an ArrayList This class called Item support the IComparable interface which contains the CompareTo method
0
8402
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8829
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5633
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2733
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1962
muto222
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.