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
Console.ReadLine()
End Sub
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
Leave a comment