Ascii Sort on a Collection in VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 533
Imports System.Collections.Generic
Imports System.Linq

Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())

Dim lstString = New List(Of String)() From { _
"14", _
"12", _
"27 Oct contri head", _
"563395", _
"565302", _
"8th Dec -12th Dec", _
"sample string 4", _
"add contribution head", _
"Navarathri", _
"Nov 5 Head Sumeru travel", _
"Product A", _
"WCF Contribution", _
"test WCF", _
"" _
}


lstString.Sort()
'default sort
'sort those records whose length > 0 e.g. "" will be ignored
'find the ascii of first charecters
'sort the records by ASCII character
'Project the sorted original string
'Add those records to the sorted result whose length = 0 e.g. include ""
lstString = lstString.Where(Function(w) w.ToCharArray().Length > 0).[Select](Function(s) New With { _
Key .OriginalString = s, _
Key .GetAsciiofFirstChar = CInt(s.ToCharArray().Take(1).[Single]()) _
}).OrderBy(Function(o) o.GetAsciiofFirstChar).[Select](Function(s) s.OriginalString).Union(lstString.Where(Function(s) s.ToCharArray().Length = 0)).ToList()
'Convert the IEnumerable to List
End Sub
End Class
End Namespace

Comments or Responses

Login to post response