Friday, April 11, 2014

Alpha Numeric Data Sort In Datatable using VB.Net

Private Function AlphaNumericDataSortInDatatable(input As DataTable, Columnname As String, SortType As String) As DataTable Try If input.Rows.Count > 0 Then Dim lobjDic As New Dictionary(Of String, List(Of Integer)) Dim IntegerData As New List(Of Integer) Dim AlpData As New List(Of String) For i As Integer = 0 To input.Rows.Count - 1 If CheckInteger(input.Rows(i)(0)) Then IntegerData.Add(CInt(input.Rows(i)(0))) Else AlpData.Add(input.Rows(i)(0)) End If Next IntegerData.Sort() lobjDic.Add("INT", IntegerData) AlpData.Sort() For Each objalp As String In AlpData Dim Key As String = "" Dim ValueObj As New List(Of Integer) For i As Integer = 0 To objalp.Length - 1 If CheckInteger(objalp.Substring(i, 1)) Then Key = objalp.Substring(0, i) If Not lobjDic.ContainsKey(Key) Then ValueObj.Add(CInt(objalp.Substring(i, objalp.Length - 1))) lobjDic.Add(Key, ValueObj) Exit For Else ValueObj = lobjDic(Key) ValueObj.Add(CInt(objalp.Substring(i, objalp.Length - 1))) Exit For End If End If Next Next For Each keyobj As String In lobjDic.Keys Dim ValueObj As New List(Of Integer) ValueObj = lobjDic(keyobj) ValueObj.Sort() Next Dim ColumnObj As New DataColumn ColumnObj.ColumnName = "Identity" ColumnObj.DataType = GetType(Integer) input.Columns.Add(ColumnObj) Dim Cnt As Integer = 1 For Each keyobj As String In lobjDic.Keys For Each valueobj As String In lobjDic(keyobj) If keyobj = "INT" Then Dim myRow() As Data.DataRow myRow = input.Select(Columnname & " = '" & valueobj & "'") myRow(0)("Identity") = Cnt Else Dim myRow() As Data.DataRow myRow = input.Select(Columnname & " = '" & keyobj & valueobj & "'") myRow(0)("Identity") = Cnt End If Cnt = Cnt + 1 Next Next Dim dvGAmt As New DataView(input) dvGAmt.Sort = "Identity " + SortType Return dvGAmt.Table End If Catch ex As Exception Return input End Try Return input End Function Private Function CheckInteger(ByVal input As String) As Boolean Dim lstrReturn As Boolean = False Try Dim intval As Integer lstrReturn = Integer.TryParse(input, intval) Catch ex As Exception Return False End Try Return lstrReturn End Function
Its sort only array like 1,A1,b1,c1,c2,2,c3,b3

No comments:

Post a Comment