Posts

Showing posts with the label Sorting

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 ...

Sort any Collection by a certain property using Insertion Sort Algorithm in C#

Sorting is a big topic to talk about , but in IT industry you need it many time every day. My leader asked me to sort a huge collection of very complicated items.My first try take about 2 mins this is catastrophe but I kept try and try till I found a nice sorting algorithm (of course for my case) which made the sorting time became about 2 secs. Insertion Sort algorithm you can find more about it here  so I will give you a sample implementation for it using C#.  you can find VB.Net version here . first, I made it for a specific datatype and a specific property which I will order by but here it is the generic version using reflection and I used this keyword to call it as extension method of my collection.. public static void SortCollection<T>(this List<T> items, String propertyName, String sortDirection = "asc") { Object tmpItem; Object value = new Object(); Object value2 = new Object(); int j; Boolean insertItem; s...