Results 1 to 3 of 3

Thread: sorting problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    2

    sorting problem

    Hi there,

    i have got the following list:

    1
    1-1
    1-2
    2
    2-1
    2-2
    2-3-1
    2-3-2
    3
    4

    How can i sort this list , that i get the following:

    1
    2
    3
    4
    1-1
    1-2
    2-1
    2-2
    2-3-1
    2-3-2


    Thnx in advance!

    Greetz chris

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: sorting problem

    Well, it looks like a two pass to me, sorting by length first, and then sorting each of those groups. The second sort looks like it would work alphabetically.
    But if you have two digit numbers between the dashes, i.e. 2-10-4, would that be sorted alphabetically (coming before 2-3-1), or numerically (coming after 2-3-2), or would that be sorted first by length (6 characters, so would be in a group after the 5 character strings).

    Or should the "-", be treated as a column separator, so 2-3-1 is three columns, and 2-10-4 is also three columns, so are part of the same group that are to be sorted together.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,301

    Re: sorting problem

    Assuming a List(Of String) and a desire to sort numerically:
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim items As New List(Of String) From {"1",
    5.                                                "1-1",
    6.                                                "1-2",
    7.                                                "2",
    8.                                                "2-1",
    9.                                                "2-2",
    10.                                                "2-3-1",
    11.                                                "2-3-2",
    12.                                                "3",
    13.                                                "4"}
    14.  
    15.         items.Sort(Function(item1, item2)
    16.                        Dim arr1 = item1.Split("-"c)
    17.                        Dim arr2 = item2.Split("-"c)
    18.  
    19.                        'Compare by length first.
    20.                        Dim result = arr1.Length.CompareTo(arr2.Length)
    21.  
    22.                        If result = 0 Then
    23.                            'Lengths are equal so compare parts.
    24.                            For i = 0 To arr1.GetUpperBound(0)
    25.                                result = CInt(arr1(i)).CompareTo(CInt(arr2(i)))
    26.  
    27.                                If result <> 0 Then
    28.                                    Exit For
    29.                                End If
    30.                            Next
    31.                        End If
    32.  
    33.                        Return result
    34.                    End Function)
    35.  
    36.         For Each item As String In items
    37.             Console.WriteLine(item)
    38.         Next
    39.  
    40.         Console.ReadLine()
    41.     End Sub
    42.  
    43. End Module
    That's using a lambda expression, which is functionally equivalent to using a named method like this:
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim items As New List(Of String) From {"1",
    5.                                                "1-1",
    6.                                                "1-2",
    7.                                                "2",
    8.                                                "2-1",
    9.                                                "2-2",
    10.                                                "2-3-1",
    11.                                                "2-3-2",
    12.                                                "3",
    13.                                                "4"}
    14.  
    15.         items.Sort(AddressOf Compare)
    16.  
    17.         For Each item As String In items
    18.             Console.WriteLine(item)
    19.         Next
    20.  
    21.         Console.ReadLine()
    22.     End Sub
    23.  
    24.     Function Compare(item1 As String, item2 As String) As Integer
    25.         Dim arr1 = item1.Split("-"c)
    26.         Dim arr2 = item2.Split("-"c)
    27.  
    28.         'Compare by length first.
    29.         Dim result = arr1.Length.CompareTo(arr2.Length)
    30.  
    31.         If result = 0 Then
    32.             'Lengths are equal so compare parts.
    33.             For i = 0 To arr1.GetUpperBound(0)
    34.                 result = CInt(arr1(i)).CompareTo(CInt(arr2(i)))
    35.  
    36.                 If result <> 0 Then
    37.                     Exit For
    38.                 End If
    39.             Next
    40.         End If
    41.  
    42.         Return result
    43.     End Function
    44.  
    45. End Module

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