473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

how to use Icomparer or IComparable to sort custom class

I have several classes that inherit other classes. I need to be able to sort
the Mesh class based on its inherited list of Poly.ZMax,. How do I use
IComparer or IComparable to achieve this?

Mitch.

Public Class Poly : Inherits List(Of Vector)

Private mZMax As Double

Public Sub New(ByVal f As List(Of Vector))

MyBase.AddRange(f)

mZMax = Me.Item(0).Z

For Each v As Vector In Me

If v.Z mZMax Then

mZMax = v.Z

End If

Next

End Sub

Public ReadOnly Property PointCount()

Get

Return MyBase.Count

End Get

End Property

Public ReadOnly Property ZMax() As Double

Get

Return mZMax

End Get

End Property

End Class

Public Class Mesh : Inherits List(Of Poly)
Public Sub AddPoly(ByVal f As Poly)

MyBase.Add(f)

End Sub

Public ReadOnly Property PolyCount()

Get

Return MyBase.Count

End Get

End Property

End Class


Sep 20 '08 #1
1 2477
I believe you want Poly to impliment IComparer(of Poly), so the declaration
becomes:

Public Class Poly : Inherits List(of Vector)
Implements IComparer(Of Poly)

Then in the Poly class, include this function:
Public Function Compare(ByVal x As Poly, ByVal y As Poly) _
As Integer Implements System.Collections.Generic.IComparer(Of
Poly).Compare
If (x.ZMax < y.ZMax) Then Return -1
If (x.ZMax y.ZMax) Then Return 1
Return 0
End Function
Now you may call MyBase.Sort() at any point necessary within your Mesh
Class.


"Mitch" <mh******@no.bother.to.spamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I have several classes that inherit other classes. I need to be able to
sort the Mesh class based on its inherited list of Poly.ZMax,. How do I use
IComparer or IComparable to achieve this?

Mitch.

Public Class Poly : Inherits List(Of Vector)

Private mZMax As Double

Public Sub New(ByVal f As List(Of Vector))

MyBase.AddRange(f)

mZMax = Me.Item(0).Z

For Each v As Vector In Me

If v.Z mZMax Then

mZMax = v.Z

End If

Next

End Sub

Public ReadOnly Property PointCount()

Get

Return MyBase.Count

End Get

End Property

Public ReadOnly Property ZMax() As Double

Get

Return mZMax

End Get

End Property

End Class

Public Class Mesh : Inherits List(Of Poly)
Public Sub AddPoly(ByVal f As Poly)

MyBase.Add(f)

End Sub

Public ReadOnly Property PolyCount()

Get

Return MyBase.Count

End Get

End Property

End Class

Sep 20 '08 #2

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

Similar topics

3
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...
1
by: Fernando Rodríguez | last post by:
Hi, I don't understand very well the diferences between both. When should each be used? Thanks
4
by: CodeRazor | last post by:
Hi, I want to retrieve a sorted list of files, ordered by LastWriteTime. I know I can implement IComparer, but I don't know how or what this means. I know a sortedlist has objects and keys and...
6
by: Robert Robbins | last post by:
Hello Programmers, I cannot figure out how to use the IComparer interface to sort an array based on the value of a double. I want to sort an array of dealers based on their distance from a zip...
3
by: RJN | last post by:
Hi I have a user defined data type which implements IComparable interface and has an implementation for the CompareTo method. Public Class MyDataType Implements IComparable --Private...
10
by: INeedADip | last post by:
I am trying to use a generic (reflection) IComparer class to sort a generic list but I get the error: Unable to cast object of type 'GenericComparer' to type 'System.Collections.Generic.Icomparer...
1
by: Brett Romero | last post by:
What are the reasons to use one over the other? SortedList implements IComparer.Compare(). Why would you also want to create a SortedList class that implements IComparable.CompareTo()? I know...
10
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...
3
dcharnigo
by: dcharnigo | last post by:
I thought I would be tricky and force the SortedList to not sort using a custom IComparer, I used the following code, but when the list is sorting using the custom sort the accessors become unusable...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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,...
0
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...
0
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
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
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,...

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.