|
-
Apr 22nd, 2006, 06:34 AM
#1
Thread Starter
Frenzied Member
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
-
Apr 22nd, 2006, 08:08 AM
#2
Re: Sorting Numerics
this sort might suit your needs. There are probably several others that could be implemented:
VB Code:
Private Sub Form_Load()
' Load the test array
Dim sArray(7) As String
sArray(0) = "H 1.txt"
sArray(1) = "H 10.txt"
sArray(2) = "H 100.txt"
sArray(3) = "H 1000.txt"
sArray(4) = "H 2.txt"
sArray(5) = "H 3.txt"
sArray(6) = "H 4.txt"
sArray(7) = "H 5.txt"
' Sort Code (Selection Sort)
Dim I As Long, J As Long, sTemp As String, N As Long
' Loop through array
For I = LBound(sArray) To UBound(sArray) - 1
For J = I + 1 To UBound(sArray)
If Val(Split(sArray(I), " ")(1)) >= Val(Split(sArray(J), " ")(1)) Then
' Swap them
sTemp = sArray(I)
sArray(I) = sArray(J)
sArray(J) = sTemp
End If
Next J
Next I
' Print Result
For I = LBound(sArray) To UBound(sArray)
Debug.Print sArray(I)
Next I
End Sub
-
Apr 22nd, 2006, 01:20 PM
#3
Thread Starter
Frenzied Member
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
-
Apr 22nd, 2006, 02:55 PM
#4
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:
Private Sub Form_Load()
Debug.Print GetNum("T--10-05.txt")
Debug.Print GetNum2("T--10-05.txt")
End Sub
Private Function GetNum(ByVal sText As String) As Long
Dim N As Long
For N = 1 To Len(sText)
If Not IsNumeric(Mid$(sText, N, 1)) Then Mid$(sText, N, 1) = Chr$(0)
Next N
GetNum = Val(Replace(sText, Chr$(0), vbNullString))
End Function
Private Function GetNum2(ByVal sText As String) As Long
Dim N As Long
For N = 1 To Len(sText)
If IsNumeric(Mid$(sText, N, 1)) Then
GetNum2 = Val(Mid$(sText, N)): Exit Function
End If
Next N
End Function
-
Apr 22nd, 2006, 04:27 PM
#5
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|