Posted by: Sourav | September 21, 2016

Sort list of complex types using delegates and lambda expressions,VB Teacher Sourav,Kolkata 09748184075


Option Strict On
Option Explicit On
Imports System
Imports System.Collections.Generic
Module Module1

Sub Main()

Dim customer1 As customer = New customer() With {.idmanipulate = 1, .namemanipulate = “Sourav”, .salarymanipulate = 30000, .typemanipulate = “Retail Customer”}
Dim customer2 As customer = New customer() With {.idmanipulate = 2, .namemanipulate = “kunal”, .salarymanipulate = 5000, .typemanipulate = “Retail Customer”}
Dim customer3 As customer = New customer() With {.idmanipulate = 3, .namemanipulate = “Subrata”, .salarymanipulate = 70000, .typemanipulate = “Retail Customer”}
Dim customer4 As customer = New customer() With {.idmanipulate = 4, .namemanipulate = “Avik”, .salarymanipulate = 9000, .typemanipulate = “Corporate Customer”}
Dim customer5 As customer = New customer() With {.idmanipulate = 5, .namemanipulate = “Manish”, .salarymanipulate = 110000, .typemanipulate = “Corporate Customer”}
Dim listcustomers As List(Of customer) = New List(Of customer)
listcustomers.Add(customer1)
listcustomers.Add(customer2)
listcustomers.Add(customer3)
listcustomers.Add(customer4)
listcustomers.Add(customer5)
Console.WriteLine(“Before Sorting”)
‘For Each c As customer In listcustomers
‘    Console.WriteLine(c.salarymanipulate)

‘Next
For Each c As customer In listcustomers
Console.WriteLine(c.namemanipulate)

Next
listcustomers.Sort()
listcustomers.Reverse()

Console.WriteLine()
Console.WriteLine()

Console.WriteLine(“After Sorting”)
‘For Each c As customer In listcustomers
‘    Console.WriteLine(c.salarymanipulate)

‘Next

For Each c As customer In listcustomers
Console.WriteLine(c.namemanipulate)

Next

‘Ok now let’s sort by name
Console.WriteLine()
Console.WriteLine()
Console.WriteLine(“Sorting manually by name”)

Dim sortbyname As sortbyname = New sortbyname
listcustomers.Sort(sortbyname)
For Each c As customer In listcustomers
Console.WriteLine(c.namemanipulate)

Next
‘Dim customercomparer As Comparison(Of customer) = New Comparison(Of customer)(AddressOf comparecustomer)
Console.WriteLine()
Console.WriteLine()
Console.WriteLine(“Before Sorting by ID”)
For Each c As customer In listcustomers
Console.WriteLine(c.idmanipulate)

Next
Console.WriteLine()
Console.WriteLine()
Console.WriteLine(“After Sorting by ID”)
‘this is done using delegates
‘listcustomers.Sort(customercomparer)
‘Ok so let’s try another simple method to achieve the same result
‘listcustomers.Sort(Function(c1 As customer, c2 As customer)

‘                       Return c1.idmanipulate.CompareTo(c2.idmanipulate)
‘                   End Function)

‘Ok let’s try achieving the same result using lambda expressions

listcustomers.Sort(Function(x, y) x.idmanipulate.CompareTo(y.idmanipulate))
For Each c As customer In listcustomers
Console.WriteLine(c.idmanipulate)

Next

Console.ReadLine()
End Sub
‘Private Function comparecustomer(ByVal x As customer, ByVal y As customer) As Integer
‘    Return x.idmanipulate.CompareTo(y.idmanipulate)

‘End Function

End Module
Public Class customer
Implements IComparable(Of customer)

Private id As Integer
Private name As String
Private salary As Integer
Private type As String

Public Property idmanipulate As Integer
Set(ByVal id As Integer)
Me.id = id
End Set
Get
Return Me.id

End Get
End Property
Public Property namemanipulate As String
Set(ByVal name As String)
Me.name = name
End Set
Get
Return Me.name

End Get
End Property
Public Property salarymanipulate As Integer
Set(ByVal salary As Integer)
Me.salary = salary
End Set
Get
Return Me.salary

End Get
End Property
Public Property typemanipulate As String
Set(ByVal type As String)
Me.type = type
End Set
Get
Return Me.type

End Get
End Property

Public Function CompareTo(ByVal other As customer) As Integer Implements IComparable(Of sortlistcomplexvb.customer).CompareTo
‘If Me.salary > other.salary Then
‘    Return 1
‘ElseIf Me.salary < other.salary Then
‘    Return -1
‘Else
‘    Return 0

‘End If
‘Return Me.salary.CompareTo(other.salary)
Return Me.name.CompareTo(other.name)

End Function

End Class

‘Ok so let’s create our own version of sorting mechanism,so that in other cases
‘for instance we like to sort by name

Public Class sortbyname
Implements IComparer(Of customer)

Public Function Compare(ByVal x As customer, ByVal y As customer) As Integer Implements System.Collections.Generic.IComparer(Of customer).Compare
Return x.namemanipulate.CompareTo(y.namemanipulate)

End Function
End Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: