-
Jun 14th, 2014, 05:08 AM
#1
Thread Starter
Member
how to sort arraylist with strings in vb.net
hi all
i want to sort an array with list of strings in vb.net
the list will start from c1 and end may be c20 or c25 or c12 etc.
but these are in random order like c2,c7,c13,c19,c10.....
i want to be in sequential order
c1, c2,c3,c4 .......c12 or c20 or c25.
i tried using array.sort(array)
but i am getting c1,c10,c11,c12...c19,c2,c21....
not like c1, c2,c3,c4 .......c12 or c20 or c25.
please guide me how to do it
thanks in advance
gvg
-
Jun 14th, 2014, 05:25 AM
#2
Re: how to sort arraylist with strings in vb.net
I'm a regex user so
vb Code:
Imports System.Text.RegularExpressions Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim list = {"c10", "c2", "c25", "c19", "c3", "c1", "c6"} Dim sorted = list.OrderBy(Function(m) OrderBy(m)) End Sub Private Function OrderBy(ByVal item As String) As Integer Return CInt(New Regex("\d+").Match(item).Value) End Function End Class
-
Jun 14th, 2014, 05:44 AM
#3
Thread Starter
Member
Re: how to sort arraylist with strings in vb.net
hi ident
thank you for your quick reply.
i am getting error in the line of code Function(m) Expression expected
Dim sorted = colgr.OrderBy(Function(m) OrderBy(m))
gvg
-
Jun 14th, 2014, 05:51 AM
#4
Re: how to sort arraylist with strings in vb.net
what framework are you targetting?
-
Jun 14th, 2014, 05:54 AM
#5
Thread Starter
Member
Re: how to sort arraylist with strings in vb.net
vb 2005 that is .netframework 2.0
-
Jun 14th, 2014, 05:56 AM
#6
Re: how to sort arraylist with strings in vb.net
Thats a LINQ method that targets 3.5
-
Jun 14th, 2014, 05:59 AM
#7
Re: how to sort arraylist with strings in vb.net
Is there a reason for targeting such an old framework?
-
Jun 14th, 2014, 06:09 AM
#8
Thread Starter
Member
Re: how to sort arraylist with strings in vb.net
i am a newbie. so i started with the old framework. later on i will shift to new framework.
-
Jun 14th, 2014, 09:59 AM
#9
Re: how to sort arraylist with strings in vb.net
Hi,
You have the ability of using an IComparer in .NET2 so keeping on the same theme as ident has already shown to split Alphabetic and Numeric characters with Regex you can combine the two solutions to get the desired result. Here is an example sorting by the Alphabetic Characters first and then by the Numeric Characters:-
vb.net Code:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim myStringArray() As String = {"b100", "c1", "db2", "eb3", "a1", "b1", "23", "a4"}
Array.Sort(myStringArray, New SortString)
MsgBox(String.Join(" ", myStringArray))
End Sub
Private Class SortString : Implements IComparer(Of String)
Public Function Compare(ByVal FirstString As String, ByVal SecondString As String) As Integer Implements IComparer(Of String).Compare
Dim stringValues As New Regex("[^\d]+")
Dim numberValues As New Regex("\d+")
If stringValues.Match(FirstString).ToString.CompareTo(stringValues.Match(SecondString).ToString) = 0 Then
Return CInt(numberValues.Match(FirstString).ToString).CompareTo(CInt(numberValues.Match(SecondString).ToString))
Else
Return stringValues.Match(FirstString).ToString.CompareTo(stringValues.Match(SecondString).ToString)
End If
End Function
End Class
End Class
This may not be the most efficient way of doing this type of Sort in .NET2 but you have to use some sort of method to split the alphabetic characters from the numeric characters to get a true ordinal sort.
Hope that helps.
Cheers,
Ian
-
Jun 14th, 2014, 10:26 AM
#10
Re: how to sort arraylist with strings in vb.net
Originally Posted by gvgbabu
i am a newbie. so i started with the old framework. later on i will shift to new framework.
I would suggest that you start with 3.5 at a minimum. There isn't any good reason to step back as far as 2.0, unless you expect users to only be running XP (which got 2.0 as a mandatory update, but only had 3.5 as a suggested update). You also might as well start out with 4 or 4.5. Every new framework adds new features, but that doesn't mean that you HAVE to use them.
My usual boring signature: Nothing
-
Jun 14th, 2014, 11:49 AM
#11
Thread Starter
Member
Re: how to sort arraylist with strings in vb.net
i am successful in splitting the string part and integer part and also sorted the integer part. Now the problem is joining the string part C to the sorted integer part. this is my code
Dim colgr As New List(Of String)
Dim colgr1 As New List(Of Integer)
For r As Integer = 0 To x1 - 1
colgr(r) = String.join("C" , colgr1(r))
Next
please guide me how to add string to integer and convert it to string
-
Jun 14th, 2014, 12:12 PM
#12
Re: how to sort arraylist with strings in vb.net
How have you managed to come up with that when we have not shown you that.
-
Jun 15th, 2014, 12:06 PM
#13
Re: how to sort arraylist with strings in vb.net
try,..
vb.net Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' add string items to list
Dim myList = New List(Of String)({"c10", "c1", "c11", "c19", "c20", "c2", "c9"})
' Sort list (ascending)
myList.Sort(New StringLogicalComparer)
' Display results
For Each item In myList
Debug.WriteLine(item)
Next
End Sub
' logical string sort , [url]http://www.pinvoke.net/default.aspx/shlwapi.strcmplogicalw[/url]
Private Class StringLogicalComparer
Implements IComparer(Of String)
Private Declare Unicode Function StrCmpLogicalW Lib "shlwapi" (ByVal s1 As String, ByVal s2 As String) As Integer
Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
Return StrCmpLogicalW(x, y) ' SortOrder.Ascending
End Function
End Class
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|