VB.Net implementation of sorting by using specified Comparer Imports System Imports System.Collections Imports System.Xml
Public Class CEmpData
Public Enum enuSortOrder FirstNameAsc = 0 FirstNameDesc = 1 LastNameAsc = 2 LastNameDesc = 3 End Enum
Private ArrEmp As ArrayList Private objEmp As CEmp
Public Sub New() InitializeData() End Sub
Private Sub InitializeData() Dim xmldoc As New XmlDocument() Dim node As XmlNode Dim objEmp As CEmp xmldoc.Load("empdata.xml") ArrEmp = New ArrayList() For Each node In xmldoc.DocumentElement.ChildNodes objEmp = New CEmp() objEmp.FName = node.SelectSingleNode("firstname").InnerText objEmp.LName = node.SelectSingleNode("lastname").InnerText ArrEmp.Add(objEmp) Next End Sub
Public Function GetSortedEmp( _ ByVal xlngSortOrder As enuSortOrder) As String ArrEmp.Sort(New CMySort(xlngSortOrder)) GetSortedEmp = GetData(ArrEmp) End Function
Private Function GetData(ByVal xArrEmp As ArrayList) As String Dim intCount As Integer Dim strEmpData As String For intCount = 0 To xArrEmp.Count - 1 strEmpData = strEmpData & CType(xArrEmp(intCount), _ CEmp).FName & Chr(9) & CType(xArrEmp(intCount), _ CEmp).LName & Chr(13) Next GetData = strEmpData End Function
Class CMySort Implements IComparer Dim lngCompType As Integer Sub New(ByVal xlngCompType As Integer) lngCompType = xlngCompType End Sub Public Function Compare(ByVal x As Object, _ ByVal y As Object) As Integer Implements _ System.Collections.IComparer.Compare Select Case lngCompType Case enuSortOrder.FirstNameAsc Compare = CType(x, CEmp).FName < CType(y, CEmp).FName Case enuSortOrder.FirstNameDesc Compare = CType(x, CEmp).FName > CType(y, CEmp).FName Case enuSortOrder.LastNameAsc Compare = CType(x, CEmp).LName < CType(y, CEmp).LName Case enuSortOrder.LastNameDesc Compare = CType(x, CEmp).LName > CType(y, CEmp).LName Case Else Compare = CType(x, CEmp).FName < CType(y, CEmp).FName End Select End Function End Class End Class
Public Class CEmp
Private mstrFName As String Private mstrLName As String
Public Property FName() As String Get FName = mstrFName End Get Set(ByVal Value As String) mstrFName = Value End Set End Property
Public Property LName() As String Get LName = mstrLName End Get Set(ByVal Value As String) mstrLName = Value End Set End Property
End Class
==============================
<employees> <employee> <firstname>ashish</firstname> <lastname>jaiman</lastname> </employee> <employee> <firstname>jaya</firstname> <lastname>pandey</lastname> </employee> <employee> <firstname>neeraj</firstname> <lastname>jaiman</lastname> </employee> <employee> <firstname>ashutosh</firstname> <lastname>sharma</lastname> </employee> <employee> <firstname>zerman</firstname> <lastname>billingslea</lastname> </employee> <employee> <firstname>bernd</firstname> <lastname>burkhardt</lastname> </employee> <employee> <firstname>sanjeev</firstname> <lastname>bhutt</lastname> </employee> <employee> <firstname>li</firstname> <lastname>li</lastname> </employee> </employees>
devcity.net
CRACKER_CODE
|