Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sort Arraylist of Objects

Posted on 2009-04-03
6
Medium Priority
?
232 Views
Last Modified: 2013-11-08
How do you sort an arraylist of objects?  See attached code.
Please provide an example using Visual Basic.
Implements IComparable
 
      Public Shared AllObjects As New ArrayList
      Public Structure AnObject
         Dim FullName as string
         Dim SSN as string
      End Structure

Open in new window

0
Comment
Question by:Ray Turner
[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
  • 3
6 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 24061630
One way...

(btw, why are you using an ArrayList if you have .Net 3.x?  Use a List(Of AnObject) instead...you can sort differently too then.)
Public Class Form1
 
    Public Structure AnObject
        Dim FullName As String
        Dim SSN As String
    End Structure
 
    Public Shared AllObjects As New ArrayList
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ao As New AnObject
        ao.FullName = "Mickey Mouse"
        ao.SSN = "123456789"
        Form1.AllObjects.Add(ao)
 
        ao = New AnObject
        ao.FullName = "Donald Duck"
        ao.SSN = "987654321"
        Form1.AllObjects.Add(ao)
 
        ao = New AnObject
        ao.FullName = "Zany Zebra"
        ao.SSN = "999999999"
        Form1.AllObjects.Add(ao)
 
        ao = New AnObject
        ao.FullName = "Goofy"
        ao.SSN = "111111111"
        Form1.AllObjects.Add(ao)
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mcc As New MyComparerClass
        Form1.AllObjects.Sort(mcc)
 
        For Each ao As AnObject In Form1.AllObjects
            Debug.Print(ao.FullName & " : " & ao.SSN)
        Next
    End Sub
 
    Private Class MyComparerClass
        Implements IComparer
 
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            If TypeOf x Is AnObject AndAlso TypeOf y Is AnObject Then
                Dim ao1 As AnObject = DirectCast(x, AnObject)
                Dim ao2 As AnObject = DirectCast(y, AnObject)
                ' comparae by FullName for instance:
                Return ao1.FullName.CompareTo(ao2.FullName)
            Else
                Return 0
            End If
        End Function
    End Class
 
End Class

Open in new window

0
 

Author Comment

by:Ray Turner
ID: 24062444
How do sort by fullname and ssn?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 24062690
You have to first check if the names are equal...and if so, instead perform the comparison on SSN.  If you really have .Net 3.X then you can sort using LINQ instead...

But here it is old-school:
Private Class MyComparerClass
        Implements IComparer
 
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            If TypeOf x Is AnObject AndAlso TypeOf y Is AnObject Then
                Dim ao1 As AnObject = DirectCast(x, AnObject)
                Dim ao2 As AnObject = DirectCast(y, AnObject)
                ' comparae by FullName for instance:
                Dim res As Integer = ao1.FullName.CompareTo(ao2.FullName)
                If res = 0 Then
                    res = ao1.SSN.CompareTo(ao2.SSN)               
                End If
                Return res
            Else
                Return 0
            End If
        End Function
    End Class

Open in new window

0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 

Author Comment

by:Ray Turner
ID: 24063608
One more question.  Is there a way to set the sort order?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24063780
Sure...set up a boolean flag in MyComparerClass to indicate ascending/descending sort order.  If we are sorting descending then we just swap "ao1" and "ao2" so the order is reversed:
Public Class Form1
 
    Public Structure AnObject
        Dim FullName As String
        Dim SSN As String
    End Structure
 
    Public Shared AllObjects As New ArrayList
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ao As New AnObject
        ao.FullName = "Mickey Mouse"
        ao.SSN = "123456789"
        Form1.AllObjects.Add(ao)
 
        ao = New AnObject
        ao.FullName = "Donald Duck"
        ao.SSN = "987654321"
        Form1.AllObjects.Add(ao)
 
        ao = New AnObject
        ao.FullName = "Zany Zebra"
        ao.SSN = "999999999"
        Form1.AllObjects.Add(ao)
 
        ao = New AnObject
        ao.FullName = "Goofy"
        ao.SSN = "111111111"
        Form1.AllObjects.Add(ao)
 
        ao = New AnObject
        ao.FullName = "Goofy"
        ao.SSN = "999999999"
        Form1.AllObjects.Add(ao)
 
        ao = New AnObject
        ao.FullName = "Goofy"
        ao.SSN = "555555555"
        Form1.AllObjects.Add(ao)
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mcc As New MyComparerClass
        mcc.SortAscending = False
        Form1.AllObjects.Sort(mcc)
 
        For Each ao As AnObject In Form1.AllObjects
            Debug.Print(ao.FullName & " : " & ao.SSN)
        Next
    End Sub
 
    Private Class MyComparerClass
        Implements IComparer
 
        Public SortAscending As Boolean = True
 
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            If TypeOf x Is AnObject AndAlso TypeOf y Is AnObject Then
                Dim ao1 As AnObject = DirectCast(x, AnObject)
                Dim ao2 As AnObject = DirectCast(y, AnObject)
 
                If Not SortAscending Then
                    ' swap the objects
                    Dim tmp As AnObject = ao1
                    ao1 = ao2
                    ao2 = tmp
                End If
 
                ' comparae by FullName for instance:
                Dim res As Integer = ao1.FullName.CompareTo(ao2.FullName)
                If res = 0 Then
                    res = ao1.SSN.CompareTo(ao2.SSN)
                End If
                Return res
            Else
                Return 0
            End If
        End Function
 
    End Class
 
End Class

Open in new window

0
 

Author Closing Comment

by:Ray Turner
ID: 31566306
Thanks!!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

704 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