Icomparer Interface

I

Irfan

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
 
J

Jon Skeet [C# MVP]

Irfan said:
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.
 
I

Irfan

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
 
J

Jon Skeet [C# MVP]

Irfan said:
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?
 
I

Irfan

thanks Skeet, this is what i have exactly done after i posted the question
to you.

Thanks again
Irfan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top