Sort array which is populated using class properties

Hi,
I'm using VS 2008 here.

I have an array:
Dim results() As JobTicketRecord

JobTicketRecord is a class which contains a number of properties such as:
string TicketNumber
string Name
string Originator.

My array results() is populated from a web service which i have no control over. I was wondering how do I sort my array results() by TicketNumber.

Any ideas?
caoimhincryanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

philipjonathanCommented:
Public Class TicketNumberComparer Implements IComparer
  Public Function Compare(x As Object, y As Object) As Integer
    Dim jx As JobTicketRecord = CType(x, JobTicketRecord)
    Dim jy As JobTicketRecord = CType(y, JobTicketRecord)
    Return jx.TicketNumber.CompareTo(jy.TicketNumber)
  End Function
End Class

Then when sorting:
Array.Sort(results, New TicketNumberComparer())
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
feradzCommented:
Hi.

The array class of .net has an overloaded method Sort that you can use it sort your array based on any criteria including TicketNumber. There are two approaches that you can follow

1. Implement the interface ICoparer interface that compares two JobTicketRecord objects based on their TicketNumber (sample is available in msdn http://msdn.microsoft.com/en-us/library/aw9s5t8f.aspx) and pass an instance of this object to the Array.Sort method

2. You can implement a delegate Comparison method that compares two JobTicketRecord objects based on the TicketNumber and pass it as an argument to Array.Sort method (sample is also available on msdn http://msdn.microsoft.com/en-us/library/cxt053xf.aspx)
0
BinuthCommented:
try this sample
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim results(2) As JobTicketRecord
        results(0) = New JobTicketRecord("C")
        results(1) = New JobTicketRecord("A")
        results(2) = New JobTicketRecord("B")
 
        Dim obj As New JobTicketRecord("")
 
        Array.Sort(results, obj)
 
    End Sub
    
 
     
End Class
 
Public Class JobTicketRecord
    Implements IComparer(Of JobTicketRecord)
 
    Public TicketNumber As String
    Sub New(ByVal s As String)
        TicketNumber = s
    End Sub
 
 
    Public Function Compare(ByVal x As JobTicketRecord, ByVal y As JobTicketRecord) As Integer Implements System.Collections.Generic.IComparer(Of JobTicketRecord).Compare
        Return x.TicketNumber.CompareTo(y.TicketNumber)
    End Function
End Class

Open in new window

0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

caoimhincryanAuthor Commented:
> philipjonathan - I am getting an error on this line in my new class:
Public Class JobTicketComparer Implements IComparer
It says end of statement expected.


> feradz and Binuth - I am looking at your solutions now.
0
feradzCommented:
philipjonathan is logically correct, probably it has some syntactical error. In which line is the error?
0
feradzCommented:
On the philipjonathan code you can try the following changes:
Public Class TicketNumberComparer
        Implements IComparer
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
            Implements IComparer.Compare
 
            Dim jx As JobTicketRecord = CType(x, JobTicketRecord)
            Dim jy As JobTicketRecord = CType(y, JobTicketRecord)
            Return jx.TicketNumber.CompareTo(jy.TicketNumber)
        End Function
    End Class

Open in new window

0
caoimhincryanAuthor Commented:
I am getting an error on this line in my new class:
Public Class JobTicketComparer Implements IComparer
It says end of statement expected.
0
feradzCommented:
Please, look at the comment with the sample code I posted above.
0
caoimhincryanAuthor Commented:
Yes feradz. That is correct. I will split the points with you and philipjonathan. Thank you very much.

I just tested this on my system and it seems to sort my array very fast. Would this be the correct practise for something like this?
0
feradzCommented:
Yes, this is the correct way to sort arrays of elements if:
1. You don't have the implementation code of the objects stored in the array and cannot implement your own compare method of the class.
2. If you want to sort your elements in an array based on more than one criteria, for example in one form based on TicketNumber and in other form based on Name.

The Sort methods in .net runtime are well optimized for a general purpose.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.