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

Icomparer Interface

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.