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

x
?
Solved

How to sort Object collection with more than one Property

Posted on 2008-10-16
5
Medium Priority
?
735 Views
Last Modified: 2012-05-05
How to sort Object collection with more than one Property

currently I am able to sort by one property thats is shelflocation but how can I implement sorting like for Shelflocation, OrderID
Means first sort by Shelflocation then OrderID

I really appreciate any code examples

Thanks
'MY CLASS WHICH IS HANDLING SORTING
    Public Class PickListLineCollection
        Inherits CollectionBase
 
        Public Function Add(ByVal NewPickListLine As PickListLine) As Integer
 
            ' If the customer doesn't already exist, add it.        
            Return InnerList.Add(NewPickListLine)
        End Function
 
#Region "Sorting the collection"
 
        Public Sub Sort(ByVal propertyName As String)
 
            If propertyName.ToUpper() = "SHELFLOCATION" Then
                ' Using Pick List Comparer to sort by Shelf Location 
                Dim Comparer As New PickListCompare
                InnerList.Sort(Comparer)
            End If
        End Sub
 
#End Region
 
    End Class
 
#Region "Code to Sort the collection"
    ' This class provides mechanism for comparing SiteRating property
    Public Class PickListCompare
        Implements IComparer
 
        Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
                            Implements IComparer.Compare
 
            Dim PickListLineY As PickListLine
            Dim PickListLineX As PickListLine
 
            ' Converting generic input objects to the objects of Product class
            PickListLineX = CType(x, PickListLine)
            PickListLineY = CType(y, PickListLine)
 
            ' Comparing SiteRating property and returing comparison result
            Return String.Compare(PickListLineX.ShelfLocation, PickListLineY.ShelfLocation)
 
        End Function
 
    End Class
 
 
 
 
'OBJECT WHICH NEED SORTING
Public Class PickListLine
 
        Private m_strShelfLocation As String
        Private m_lngOrderID As Long
        Private m_strCustomer As String
        Private m_strProductName As String
 
        Public Sub New(ByVal ShelfLocation As String, _
                       ByVal OrderID As Long, _
                       ByVal Customer As String, _
                       ByVal ProductName As String, _
                       )
 
            m_strShelfLocation = ShelfLocation
            m_lngOrderID = OrderID
            m_strCustomer = Customer
            m_strProductName = ProductName
 
        End Sub
 
        Public ReadOnly Property ShelfLocation() As String
            Get
                Return m_strShelfLocation
            End Get
        End Property
 
 
        Public ReadOnly Property OrderID() As Long
            Get
                Return m_lngOrderID
            End Get
        End Property
 
        Public ReadOnly Property CustomerName() As String
            Get
                Return m_strCustomer
            End Get
        End Property
 
 
        Public ReadOnly Property ProductName() As String
            Get
                Return m_strProductName
            End Get
        End Property
 
    End Class

Open in new window

0
Comment
Question by:shieldguy
[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
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 22733594
In your Compare() method, store the result of the first comparsion in a local variable.  If they are equal then do the second comparison and return that instead:

        Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
                            Implements IComparer.Compare
 
            Dim PickListLineY As PickListLine
            Dim PickListLineX As PickListLine
 
            ' Converting generic input objects to the objects of Product class
            PickListLineX = CType(x, PickListLine)
            PickListLineY = CType(y, PickListLine)
 
            ' Comparing SiteRating property and returing comparison result
            Dim result As Integer = PickListLineX.ShelfLocation.CompareTo(PickListLineY.ShelfLocation)
            If result = 0 Then
                result = PickListLineX.OrderID.CompareTo(PickListLineY.OrderID)
            End If
            Return result
        End Function
0
 
LVL 3

Expert Comment

by:JunkMan
ID: 22736097
That was exactly one of the reasons i upgraded most of my projects to 3.5...

Linq is a beauty :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22736145
@JunkMan,

Yeah, I'm sure Linq can handle it quite nicely in short code that is easy to read and understand.

I'd love to play with it...can you buy me a new laptop so I can get cracking?...  =)
0
 
LVL 3

Expert Comment

by:JunkMan
ID: 22736297
@Idle_Mind,

I'm sure a new laptop wouldn't be required just for an upgrade to vs08! In any case here you go, it looks pretty new to me!..
17laptop-l.jpg
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22736404
Thanx!  Unfortunately my replicator is down...maybe I can whip one of those up tomorrow...  ;)
Replicator.jpg
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 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