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?
 
philipjonathanConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
 
feradzConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.