pcavacas,
It sounds like your already familiar with this stuff, but for a quick review, or for other readers:
HOW TO: Use the IComparable and the IComparer Interfaces in Visual Basic .NET
http://support.microsoft.c
Main Topics
Browse All TopicsI have a class that inherits from collection base and I want to implement a custom comparing mechanism with the sort routine.
So in my collection I want to have something like
Sort(columnIndex as int32, sortOrder as ComponentModel.ListSortOrd
Now I know that I can create a class that implements an IComparer interface and pass this class into the Sort method of the InnerList property of the CollectionBase class and have tried this, but it is not ever calling my comparing code.
Here is what I have:
Public Class NodeCollection
Inherits CollectionBase
Public Sub Sort(ByVal columnIndex As Int32, ByVal sortOrder As System.ComponentModel.List
Dim sorter As New Node.AdvancedSort
sorter.ColumnIndex = columnIndex
sorter.ListSortOrder = sortOrder
MyBase.InnerList.Sort(sort
End Sub
End Class
Public Class Node
Public Class AdvancedSort
Implements System.Collections.ICompar
Private _ColumnIndex As Int32
Private _ListSortOrder As System.ComponentModel.List
Public Property ColumnIndex() As Int32
Get
Return _ColumnIndex
End Get
Set(ByVal Value As Int32)
_ColumnIndex = Value
End Set
End Property
Public Property ListSortOrder() As System.ComponentModel.List
Get
Return _ListSortOrder
End Get
Set(ByVal Value As System.ComponentModel.List
_ListSortOrder = Value
End Set
End Property
Public Function Compare(ByVal x As Object, ByVal y As Object) As Int32 Implements System.Collections.ICompar
'My logic to sort is here
End Sub
End Class
End Class
Nowif I step through the code in the Sort method and put a breakpoint or any code (like throwing an exception) in the Compare method that code is never executed and my collection is not being sorted
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
pcavacas,
It sounds like your already familiar with this stuff, but for a quick review, or for other readers:
HOW TO: Use the IComparable and the IComparer Interfaces in Visual Basic .NET
http://support.microsoft.c
Your example worked. Also if I copy out the relevant portions code from my project into another project I can get it to work there as well. But for some reason when it is in my project it is not working. Stepping through the code I walk right through the Sort method where it creates an instance of AdvancedSort class and calls the sort on the Innerlist. It just never goes into the Cmopare method.
Given that, I'd assume the project is somehow corrupted.
Given that your project's named: Xxxx
Rename your project to OldXxxx
Remove it from your solution ( Don't delete it! )
Create a new project Xxxx in your solution.
Create new files in the new project as needed.
Copy-and-paste all your code from the old project to the new project.
I don't have any better ideas than that. I hope it works.
Business Accounts
Answer for Membership
by: farsightPosted on 2004-03-29 at 20:11:17ID: 10710455
I'm not sure why you're having a problem. I had no problem. This code works fine for me. (Win2K, VS.NET 2003)
SortDirect ion.Ascend ing)
SortDirect ion.Ascend ing)
SortDirect ion) er)
})", column1, column2)
er
SortDirect ion
SortDirect ion SortDirect ion)
er.Compare
ncedSort is improperly configured: ColumnIndex.") SortDirect ion.Descen ding Then
I just started with your code, added several things:
TrySortCollection.Test -- to setup a list of nodes and call sorts. (I'm actually testing by eyeballing the output.)
TrySortCollection -- utils for random numbers
NodeCollection.Add
Node.column1
Node.column2
Node constructor New
Node.ToString
AdvancedSort.Compare -- added an implementation for the existing function.
Try it! How's it different from what you have?
If my code doesn't work on your machine, we have a real mystery brewing here!
[VB.NET]
Option Explicit On
Option Strict On
Public Class TrySortCollection
Private Shared r As New System.Random
Public Shared Sub Test()
Dim size As Integer = 4
Dim nc As New NodeCollection
For i As Integer = 1 To size
nc.Add(New Node(GetRnd(), GetRnd()))
Next
' Sort by 1st column, and show.
nc.Sort(1, System.ComponentModel.List
For Each n As Node In nc
Debug.WriteLine("1 -- " & n.ToString())
Next
' Sort by 2nd column, and show.
nc.Sort(2, System.ComponentModel.List
For Each n As Node In nc
Debug.WriteLine("2 -- " & n.ToString())
Next
End Sub
Private Shared Function GetRnd() As Integer
Return r.Next(0, 100)
End Function
End Class
Public Class NodeCollection
Inherits CollectionBase
Public Sub Sort(ByVal columnIndex As Int32, ByVal sortOrder As System.ComponentModel.List
Dim sorter As New Node.AdvancedSort
sorter.ColumnIndex = columnIndex
sorter.ListSortOrder = sortOrder
MyBase.InnerList.Sort(sort
End Sub
Public Sub Add(ByVal node As Node)
MyBase.InnerList.Add(node)
End Sub
End Class
Public Class Node
Public ReadOnly column1 As Integer
Public ReadOnly column2 As Integer
Public Sub New(ByVal col1 As Integer, ByVal col2 As Integer)
column1 = col1
column2 = col2
End Sub
Public Overrides Function ToString() As String
Return String.Format("Node({0},{1
End Function
Public Class AdvancedSort
Implements System.Collections.ICompar
Private _ColumnIndex As Int32
Private _ListSortOrder As System.ComponentModel.List
Public Property ColumnIndex() As Int32
Get
Return _ColumnIndex
End Get
Set(ByVal Value As Int32)
_ColumnIndex = Value
End Set
End Property
Public Property ListSortOrder() As System.ComponentModel.List
Get
Return _ListSortOrder
End Get
Set(ByVal Value As System.ComponentModel.List
_ListSortOrder = Value
End Set
End Property
Public Function Compare(ByVal x As Object, ByVal y As Object) As Int32 Implements System.Collections.ICompar
'My logic to sort is here
Dim result As Integer
Dim xNode As Node = DirectCast(x, Node)
Dim yNode As Node = DirectCast(y, Node)
Select Case _ColumnIndex
Case 1
If xNode.column1 > yNode.column1 Then
result = 1
ElseIf xNode.column1 < yNode.column1 Then
result = -1
Else
result = 0
End If
Case 2
If xNode.column2 > yNode.column2 Then
result = 1
ElseIf xNode.column2 < yNode.column2 Then
result = -1
Else
result = 0
End If
Case Else
Throw New ApplicationException("Adva
End Select
If _ListSortOrder = System.ComponentModel.List
result = -result
End If
Return result
End Function
End Class
End Class