473,505 Members | 13,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Icomparer Interface

hi,

I am trying to arrange a CollectionOfCars in increasing order of their
X-Cordinate location.
CollectionOfCars is a generic collection.
Dim CollectionOfCars 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

CollectionOfCars.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 1396
Irfan <ir***@asc-ltd.co.uk> wrote:
I am trying to arrange a CollectionOfCars in increasing order of their
X-Cordinate location.
CollectionOfCars is a generic collection.
Dim CollectionOfCars 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

CollectionOfCars.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
"CarComparer" 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.com>
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.Add(ocar)
CarCollection.Add(ocar1)
CarCollection.Add(ocar2)
CarCollection.Add(ocar3)
CarCollection.Sort(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 WindowsApplication4.car).Compare
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Irfan <ir***@asc-ltd.co.uk> wrote:
I am trying to arrange a CollectionOfCars in increasing order of their
X-Cordinate location.
CollectionOfCars is a generic collection.
Dim CollectionOfCars 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

CollectionOfCars.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
"CarComparer" 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.com>
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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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
2459
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 -...
3
30162
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...
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 -...
3
2355
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...
10
2515
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...
0
7213
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
7298
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,...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
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...
0
5610
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
5026
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
4698
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
1526
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 ...
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.