Results 1 to 13 of 13

Thread: how to sort arraylist with strings in vb.net

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    58

    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

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: how to sort arraylist with strings in vb.net

    I'm a regex user so

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim list = {"c10", "c2", "c25", "c19", "c3", "c1", "c6"}
    7.         Dim sorted = list.OrderBy(Function(m) OrderBy(m))
    8.     End Sub
    9.  
    10.     Private Function OrderBy(ByVal item As String) As Integer
    11.         Return CInt(New Regex("\d+").Match(item).Value)
    12.     End Function
    13. End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    58

    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

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: how to sort arraylist with strings in vb.net

    what framework are you targetting?

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    58

    Re: how to sort arraylist with strings in vb.net

    vb 2005 that is .netframework 2.0

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: how to sort arraylist with strings in vb.net

    Thats a LINQ method that targets 3.5

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: how to sort arraylist with strings in vb.net

    Is there a reason for targeting such an old framework?

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    58

    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.

  9. #9
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.   Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    6.     Dim myStringArray() As String = {"b100", "c1", "db2", "eb3", "a1", "b1", "23", "a4"}
    7.  
    8.     Array.Sort(myStringArray, New SortString)
    9.  
    10.     MsgBox(String.Join(" ", myStringArray))
    11.   End Sub
    12.  
    13.   Private Class SortString : Implements IComparer(Of String)
    14.     Public Function Compare(ByVal FirstString As String, ByVal SecondString As String) As Integer Implements IComparer(Of String).Compare
    15.       Dim stringValues As New Regex("[^\d]+")
    16.       Dim numberValues As New Regex("\d+")
    17.  
    18.       If stringValues.Match(FirstString).ToString.CompareTo(stringValues.Match(SecondString).ToString) = 0 Then
    19.         Return CInt(numberValues.Match(FirstString).ToString).CompareTo(CInt(numberValues.Match(SecondString).ToString))
    20.       Else
    21.         Return stringValues.Match(FirstString).ToString.CompareTo(stringValues.Match(SecondString).ToString)
    22.       End If
    23.     End Function
    24.   End Class
    25. 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

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,990

    Re: how to sort arraylist with strings in vb.net

    Quote Originally Posted by gvgbabu View Post
    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

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    58

    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

  12. #12
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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.

  13. #13
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: how to sort arraylist with strings in vb.net

    try,..

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    4.         ' add string items to list
    5.         Dim myList = New List(Of String)({"c10", "c1", "c11", "c19", "c20", "c2", "c9"})
    6.         ' Sort list (ascending)
    7.         myList.Sort(New StringLogicalComparer)
    8.         ' Display results
    9.         For Each item In myList
    10.             Debug.WriteLine(item)
    11.         Next
    12.     End Sub
    13.  
    14.     ' logical string sort , [url]http://www.pinvoke.net/default.aspx/shlwapi.strcmplogicalw[/url]
    15.     Private Class StringLogicalComparer
    16.         Implements IComparer(Of String)
    17.         Private Declare Unicode Function StrCmpLogicalW Lib "shlwapi" (ByVal s1 As String, ByVal s2 As String) As Integer
    18.         Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
    19.             Return StrCmpLogicalW(x, y) ' SortOrder.Ascending
    20.         End Function
    21.     End Class
    22.  
    23. 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
  •  



Click Here to Expand Forum to Full Width