Results 1 to 5 of 5

Thread: Sorting Numerics

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Sorting Numerics

    What is the easiest way to sort this properly?

    H 1.txt
    H 10.txt
    H 100.txt
    H 1000.txt
    H 2.txt
    H 3.txt
    H 4.txt
    H 5.txt


    Want it to be like this.
    H 1.txt
    H 2.txt
    H 3.txt
    H 4.txt
    H 5.txt
    H 10.txt
    H 100.txt
    H 1000.txt
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Sorting Numerics

    this sort might suit your needs. There are probably several others that could be implemented:
    VB Code:
    1. Private Sub Form_Load()
    2.     ' Load the test array
    3.     Dim sArray(7) As String
    4.    
    5.     sArray(0) = "H 1.txt"
    6.     sArray(1) = "H 10.txt"
    7.     sArray(2) = "H 100.txt"
    8.     sArray(3) = "H 1000.txt"
    9.     sArray(4) = "H 2.txt"
    10.     sArray(5) = "H 3.txt"
    11.     sArray(6) = "H 4.txt"
    12.     sArray(7) = "H 5.txt"
    13.    
    14.     ' Sort Code (Selection Sort)
    15.     Dim I As Long, J As Long, sTemp As String, N As Long
    16.    
    17.     ' Loop through array
    18.     For I = LBound(sArray) To UBound(sArray) - 1
    19.         For J = I + 1 To UBound(sArray)
    20.             If Val(Split(sArray(I), " ")(1)) >= Val(Split(sArray(J), " ")(1)) Then
    21.                 ' Swap them
    22.                 sTemp = sArray(I)
    23.                 sArray(I) = sArray(J)
    24.                 sArray(J) = sTemp
    25.             End If
    26.         Next J
    27.     Next I
    28.    
    29.     ' Print Result
    30.     For I = LBound(sArray) To UBound(sArray)
    31.         Debug.Print sArray(I)
    32.     Next I
    33. End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Sorting Numerics

    Hey bushmobile,

    You are splitting it on a space. What if the filename contains no spaces?

    I want to be able to sort any set of files.

    Today, it might be:
    H 1.txt
    H 10.txt
    H 100.txt
    H 5.txt

    Tomorrow, it might be:
    T--1.txt
    T--10.txt
    T--100.txt
    T--5.txt
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Sorting Numerics

    you'll have to make a function to get the Number from the string. I make no claims that these are the fastest ways.

    There are two different functions because they do slightly different things. The example will show you:
    VB Code:
    1. Private Sub Form_Load()
    2.     Debug.Print GetNum("T--10-05.txt")
    3.     Debug.Print GetNum2("T--10-05.txt")
    4. End Sub
    5.  
    6. Private Function GetNum(ByVal sText As String) As Long
    7.     Dim N As Long
    8.     For N = 1 To Len(sText)
    9.         If Not IsNumeric(Mid$(sText, N, 1)) Then Mid$(sText, N, 1) = Chr$(0)
    10.     Next N
    11.     GetNum = Val(Replace(sText, Chr$(0), vbNullString))
    12. End Function
    13.  
    14. Private Function GetNum2(ByVal sText As String) As Long
    15.     Dim N As Long
    16.     For N = 1 To Len(sText)
    17.         If IsNumeric(Mid$(sText, N, 1)) Then
    18.             GetNum2 = Val(Mid$(sText, N)): Exit Function
    19.         End If
    20.     Next N
    21. End Function

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Sorting Numerics

    Thank You, I will try it and let you know.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

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