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