• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

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?
0
caoimhincryan
Asked:
caoimhincryan
2 Solutions
 
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
 
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now