Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort array of structures...

Status
Not open for further replies.

Programmer76

Programmer
Jul 25, 2006
35
CA
Hi,

I am new to VB.NET and am looking for a way to sort and array of structures.

Example:

I have a structure that contains first name, last name, and age. I would like to be able to sort on any of those propertise.

Any suggestions?

CES
 
Thanks,

How would I go about finding this thread or "asking Chrissie"?

CES
 
Well, the search function on this site isn't all that great, but I clicked on Chrissie1 over there on the right -->

Then I click on his posts in the Vb.Net forum. And a little ways down on the first page was a thread titled "Compare two objects". It might not be exactly what you were asking for, but Chrissie posted about implimenting iComparible, which is probably a great place to start looking.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I know structures are suppossed to be faster (heap and stack) then classes. But I think classes have so much more to offer then the structures, they are much more powerfull. I never use structs anymore.

Christiaan Baes
Belgium

"My new site" - Me
 
Thanks for the input...I am just starting .NET and can use all I can get.

I have used Structures in this case as a requirement for a project I am doing for night school.

Though I do not believe that an interface was a requirement for the project I was able to implement the IComparer interface to sort the ArrayList of structures and the whole concept really great!!

I hope the more that I get into .NET the better understanding I will have of interfaces. Are they typically a little foggy when you first start to use them?

Do you know of any good articles to explain about this from the basics up?

CES
 
Interfaces are actually real easy. Consider them to be a template you have to follow to make your class/strucure. Making interfaces is easy. You just have to say wich methods the interface needs (no implementation. Then the class implements the interface, this means that the class will have to have those methods and will "need" (There is no way to know if the developer really did this or not) to implement them. This also means that a class that implements a certain interface also is of this type.

For example classA implments Iinterface and ClassB does the same

Iinterface looks like this

Code:
Public Interface Iinterface
  public sub methodname(byval myparam as object)
end interface

So if you have another class that you want to accept classA or ClassB as a parameter you can easily do this because they both implement Iinterface so the method will look like this

Code:
private sub mymethod(byval param as Iinterface)
    param.methodname
end sub

Interfaces are also(mainly) used in multiprogrammer inveronments to enforce certain rules.

It all has to do with Object Oriented side of things and Polymorphism

I bet the other guys would like to add some things.


Christiaan Baes
Belgium

"My new site" - Me
 
For most of my stuff I just use inheritance and overriding. But there are some methods that MUST be writen in the child classes. Instead of depending on the future developers to read the documentation and know what methods and signatures they need, we can use and implimentation to force them to write those methods.

My only annoyance is that 2k2/2k3 just gives you error messages if you impliment a class in a new class. I always wished it would just creat the method signatures for you. Does 2k5 do that?

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
So you cannot sort an array of structures because it is not a reference type?
 
Hi,

I am brand new to VB.NET so aside from this being messing here is an example of how to go about sorting an arraylist of client structures.

You can create your own class that implements the IComparer interface for example which is going to tell the arraylist sort method how to sort the structure:

Code:
Public Class ClientComparer
    Implements IComparer

    Private mSortProperty As String
    Private mSortOrder As SortOrder

    Public Sub New(ByVal SortProperty As String, Optional ByVal SortOrder As SortOrder = SortOrder.Ascending)
        mSortProperty = SortProperty

        mSortOrder = SortOrder
    End Sub

    'Compare the two employees.
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim ClientX As Client.Client = DirectCast(x, Client.Client)
        Dim ClientY As Client.Client = DirectCast(y, Client.Client)

        Dim strTempX As String
        Dim strTempY As String

        Dim intComparison As Integer

        'CHECK WHICH PROPERTY WE ARE SORTING ON
        If mSortProperty = "First" Then
            strTempX = ClientX.FirstName & ClientX.LastName
            strTempY = ClientY.FirstName & ClientY.LastName

            If mSortOrder = SortOrder.Ascending Then
                intComparison = String.Compare(strTempX, strTempY)
            Else
                intComparison = String.Compare(strTempY, strTempX)
            End If

        ElseIf mSortProperty = "Last" Then
            strTempX = ClientX.LastName & ClientX.FirstName
            strTempY = ClientY.LastName & ClientY.FirstName

            If mSortOrder = SortOrder.Ascending Then
                intComparison = String.Compare(strTempX, strTempY)
            Else
                intComparison = String.Compare(strTempY, strTempX)
            End If

        ElseIf mSortProperty = "Age" Then

            'CHECK IF WE ARE SORTING IN ASCENDING OR DESCENDING ORDER
            If mSortOrder = SortOrder.Ascending Then
                intComparison = Date.Compare(ClientX.DOB, ClientY.DOB)
            Else
                intComparison = Date.Compare(ClientY.DOB, ClientX.DOB)
            End If

        End If

        'RETURN THE RESULT OF THE COMPARISON
        Return intComparison

    End Function

End Class

You need a client structure with a minium of the following properties for this example:

First name
Last name
Age

In your code create an object of the comparer class, above.

Then use the sort method of the arraylist object and tell it how to sort the arraylist by passing in the IComparer object which is where you define how to sort the structure.

Code:
    'DECLARE COMPARER CLASS
    Dim myClientComparer As New ClientComparer("Age", SortOrder.Descending)

    'SORT THE ARRAYLIST AND PASS IN YOUR COMPARER OBJECT
    ClentList.Sort(myclientcomparer)

What happens is the sort method of the arraylist uses the compare function that I defined IComparer class and this tells it how to commpere the objects.

The function simply returns a -1, 0, or 1to the calling funtion. Less than, equal to, or greater than.

I know this is messy but I hope it helps. If you have any questions regarding my example please ask.

Aside from it being a messy example, what do the experts think? I know I have alot to learn, just started not too long ago.

It looks like a lot but this is really simple!

CES
 
Not to bad I just don't like this

optional parameters Are used less and less most people will use overloading.

Code:
Public Sub New(ByVal SortProperty As String, Optional ByVal SortOrder As SortOrder = SortOrder.Ascending)
        mSortProperty = SortProperty

        mSortOrder = SortOrder
    End Sub

try this instead

Code:
Public Sub New(ByVal SortProperty As String)
        me.new(SortProperty,sortorder.ascending)
    End Sub

Public Sub New(ByVal SortProperty As String, ByVal SortOrder As SortOrder)
        mSortProperty = SortProperty
        mSortOrder = SortOrder
    End Sub



Christiaan Baes
Belgium

"My new site" - Me
 
Good call! I like the look of that better.

What do you gain from this method over the optional parameters? Is it just becoming more of a standard?

CES
 
Have a good weekend.

I look forward to both of your posts in the near future.

Thanks,

CES
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top