virtuoso13
Technical User
Hi all,
I have a problem in sorting array of structure. I found out a thread here talking about the same thing and it had been closed long time ago. I applied the codes and it does not seem to work.
Here's my code:
-- 1. I created an Icomparer class:
Public Class B2BLineComparer
Implements IComparer
Private mSortProperty As String
Private mSortOrder As SortOrder
Public Sub New(ByVal SortProperty As String, ByVal SortOrder As SortOrder)
Me.mSortProperty = SortProperty
Me.mSortOrder = SortOrder
End Sub
'Compare the two B2BLines.
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim ClientX As AC_SO.B2BLines = DirectCast(x, AC_SO.B2BLines)
Dim ClientY As AC_SO.B2BLines = DirectCast(y, AC_SO.B2BLines)
Dim strTempX As String
Dim strTempY As String
Dim intComparison As Integer
'CHECK WHICH PROPERTY WE ARE SORTING ON
If mSortProperty = "CustomerCode" Then
strTempX = ClientX.LnCardCode
strTempY = ClientY.LnCardCode
If mSortOrder = SortOrder.Ascending Then
intComparison = String.Compare(strTempX, strTempY)
Else
intComparison = String.Compare(strTempY, strTempX)
End If
ElseIf mSortProperty = "WarehouseCode" Then
strTempX = ClientX.LnWhseCode
strTempY = ClientY.LnWhseCode
If mSortOrder = SortOrder.Ascending Then
intComparison = String.Compare(strTempX, strTempY)
Else
intComparison = String.Compare(strTempY, strTempX)
End If
End If
'RETURN THE RESULT OF THE COMPARISON
Return intComparison
End Function
End Class
-- 2. I have defined a structure in another class AC_SO:
Public Structure B2BLines
Public LnCardCode As String
Public LnWhseCode As String
Public LnItemCode As String
Public LnCurrency As String
Public LnQuantity As Double
Public LnNetPrice As Double
Public LnDiscount As Double
End Structure
-- 3. I declare array of structure:
Private B2BLine(100) As B2BLines
-- 4. I filled data into the array.
-- 5. I'm sorting it:
'DECLARE COMPARER CLASS
Dim myComparer As New B2BLineComparer("Customer", SortOrder.Ascending)
'SORT THE ARRAYLIST AND PASS IN YOUR COMPARER OBJECT
Array.Sort(B2BLine, myComparer)
The problem is i do not understand how does the comparer work and in my case, it's not working properly. Is it something wrong with the codes? Or it's because im using Visual Studio 2003 in which i cannot directly use:
B2BLine.Sort(myComparer)
to sort the array of structure?
Please advise.
Thanks for your attention.
cheers,
erwine
I have a problem in sorting array of structure. I found out a thread here talking about the same thing and it had been closed long time ago. I applied the codes and it does not seem to work.
Here's my code:
-- 1. I created an Icomparer class:
Public Class B2BLineComparer
Implements IComparer
Private mSortProperty As String
Private mSortOrder As SortOrder
Public Sub New(ByVal SortProperty As String, ByVal SortOrder As SortOrder)
Me.mSortProperty = SortProperty
Me.mSortOrder = SortOrder
End Sub
'Compare the two B2BLines.
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim ClientX As AC_SO.B2BLines = DirectCast(x, AC_SO.B2BLines)
Dim ClientY As AC_SO.B2BLines = DirectCast(y, AC_SO.B2BLines)
Dim strTempX As String
Dim strTempY As String
Dim intComparison As Integer
'CHECK WHICH PROPERTY WE ARE SORTING ON
If mSortProperty = "CustomerCode" Then
strTempX = ClientX.LnCardCode
strTempY = ClientY.LnCardCode
If mSortOrder = SortOrder.Ascending Then
intComparison = String.Compare(strTempX, strTempY)
Else
intComparison = String.Compare(strTempY, strTempX)
End If
ElseIf mSortProperty = "WarehouseCode" Then
strTempX = ClientX.LnWhseCode
strTempY = ClientY.LnWhseCode
If mSortOrder = SortOrder.Ascending Then
intComparison = String.Compare(strTempX, strTempY)
Else
intComparison = String.Compare(strTempY, strTempX)
End If
End If
'RETURN THE RESULT OF THE COMPARISON
Return intComparison
End Function
End Class
-- 2. I have defined a structure in another class AC_SO:
Public Structure B2BLines
Public LnCardCode As String
Public LnWhseCode As String
Public LnItemCode As String
Public LnCurrency As String
Public LnQuantity As Double
Public LnNetPrice As Double
Public LnDiscount As Double
End Structure
-- 3. I declare array of structure:
Private B2BLine(100) As B2BLines
-- 4. I filled data into the array.
-- 5. I'm sorting it:
'DECLARE COMPARER CLASS
Dim myComparer As New B2BLineComparer("Customer", SortOrder.Ascending)
'SORT THE ARRAYLIST AND PASS IN YOUR COMPARER OBJECT
Array.Sort(B2BLine, myComparer)
The problem is i do not understand how does the comparer work and in my case, it's not working properly. Is it something wrong with the codes? Or it's because im using Visual Studio 2003 in which i cannot directly use:
B2BLine.Sort(myComparer)
to sort the array of structure?
Please advise.
Thanks for your attention.
cheers,
erwine