Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sorting

Posted on 2004-03-29
6
Medium Priority
?
813 Views
Last Modified: 2012-05-04
I 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.ListSortOrder)

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.ListSortDirection)
            Dim sorter As New Node.AdvancedSort
            sorter.ColumnIndex = columnIndex
            sorter.ListSortOrder = sortOrder
            MyBase.InnerList.Sort(sorter)
        End Sub

End Class

Public Class Node

    Public Class AdvancedSort
            Implements System.Collections.IComparer

            Private _ColumnIndex As Int32
            Private _ListSortOrder As System.ComponentModel.ListSortDirection

            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.ListSortDirection
                Get
                    Return _ListSortOrder
                End Get
                Set(ByVal Value As System.ComponentModel.ListSortDirection)
                    _ListSortOrder = Value
                End Set
            End Property

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Int32 Implements System.Collections.IComparer.Compare
               '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
0
Comment
Question by:pcavacas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 12

Expert Comment

by:farsight
ID: 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)

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.ListSortDirection.Ascending)
        For Each n As Node In nc
            Debug.WriteLine("1 -- " & n.ToString())
        Next

        ' Sort by 2nd column, and show.
        nc.Sort(2, System.ComponentModel.ListSortDirection.Ascending)
        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.ListSortDirection)
        Dim sorter As New Node.AdvancedSort
        sorter.ColumnIndex = columnIndex
        sorter.ListSortOrder = sortOrder
        MyBase.InnerList.Sort(sorter)
    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})", column1, column2)
    End Function

    Public Class AdvancedSort
        Implements System.Collections.IComparer

        Private _ColumnIndex As Int32
        Private _ListSortOrder As System.ComponentModel.ListSortDirection

        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.ListSortDirection
            Get
                Return _ListSortOrder
            End Get
            Set(ByVal Value As System.ComponentModel.ListSortDirection)
                _ListSortOrder = Value
            End Set
        End Property

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Int32 Implements System.Collections.IComparer.Compare
            '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("AdvancedSort is improperly configured: ColumnIndex.")
            End Select
            If _ListSortOrder = System.ComponentModel.ListSortDirection.Descending Then
                result = -result
            End If
            Return result
        End Function
    End Class
End Class
0
 
LVL 12

Expert Comment

by:farsight
ID: 10710461
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.com/default.aspx?scid=kb;EN-US;q321292
0
 
LVL 2

Author Comment

by:pcavacas
ID: 10713715
  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.
0
 
LVL 12

Accepted Solution

by:
farsight earned 2000 total points
ID: 10717948
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.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question