Sort any Collection by a certain property using Insertion Sort Algorithm in VB.Net

This post is a just a Vb.Net Version of the code explained in this POST
<Extension()> _
Public Sub SortCollection(Of T)(ByRef items As List(Of T), ByVal propertyName As String, Optional ByVal sortDirection As String = "asc")
    Dim tmpItem As Object
    Dim value As New Object
    Dim value2 As New Object
    Dim j As Integer
    Dim insertItem As Boolean
    Select Case sortDirection.ToLower
        Case "asc"
            Try
                If items.Count > 1 Then
                    Dim itemType As String = items(0).GetType().GetProperty(propertyName).GetValue(items(0), Nothing).GetType().ToString()
                    For i As Integer = 1 To items.Count - 1
                        j = i - 1
                        insertItem = False
                        tmpItem = items(i)
                        value = items(i).GetType().GetProperty(propertyName).GetValue(items(i), Nothing)
                        Do Until insertItem
                            value2 = items(j).GetType().GetProperty(propertyName).GetValue(items(j), Nothing)
                            If MyComparer(value2, value, itemType) = 1 Then
                                items(j + 1) = items(j)
                                j = j - 1
                                If j < 0 Then
                                    insertItem = True
                                End If
                            Else
                                insertItem = True
                            End If
                        Loop
                        items(j + 1) = tmpItem
                    Next
                End If
            Catch ex As Exception

            End Try
        Case "desc"
            Try
                If items.Count > 1 Then
                    Dim itemType As String = items(0).GetType().GetProperty(propertyName).GetValue(items(0), Nothing).GetType().ToString()
                    For i As Integer = 1 To items.Count - 1
                        j = i - 1
                        insertItem = False
                        tmpItem = items(i)
                        value = items(i).GetType().GetProperty(propertyName).GetValue(items(i), Nothing)
                        Do Until insertItem
                            value2 = items(j).GetType().GetProperty(propertyName).GetValue(items(j), Nothing)
                            If MyComparer(value2, value, itemType) = -1 Then
                                items(j + 1) = items(j)
                                j = j - 1
                                If j < 0 Then
                                    insertItem = True
                                End If
                            Else
                                insertItem = True
                            End If
                        Loop
                        items(j + 1) = tmpItem
                    Next
                End If
            Catch ex As Exception

            End Try
    End Select
End Sub
Private Function MyComparer(ByVal secondVal As Object, ByVal firstVal As Object, ByVal itemType As String) As Integer
    Dim result As Integer = 0
    Select Case itemType
        Case "System.Int16", "System.Int32", "System.Int64"
            If secondVal > firstVal Then
                Return 1
            ElseIf secondVal < firstVal Then
                Return -1
            Else
                Return 0
            End If
        Case "System.String", "String"
            Return CStr(secondVal).CompareTo(CStr(firstVal))
        Case "System.DateTime", "System.Date", "DateTime"
            Return CDate(secondVal).CompareTo(CDate(firstVal))
    End Select
End Function

Comments

Popular posts from this blog

ASP.Net MVC : ActionLink with bootstrap icon

ASP.Net MVC : Conditional Validation using ValidationAttribute

Android : How to change progress bar color at runtime programmatically?