|
-
Nov 4th, 2007, 05:41 PM
#1
Thread Starter
Addicted Member
[RESOLVED] How to sort string?
Dear all expert,
Please tell me how to sort string from "9874056213" to "0123456789".
Thank you for all post.
-
Nov 4th, 2007, 05:46 PM
#2
Re: How to sort string?
Read each character and build a new string with the lowest character first etc. Use Mid$ and Instr in a loop to read the characters.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 4th, 2007, 07:35 PM
#3
Re: How to sort string?
Code:
Const TESTDATA = "9874056213"
Dim strNew As String
Dim lngIndex As Long
Dim lngIndexNew As Long
strNew = Mid$(TESTDATA, 1, 1)
lngIndex = 2
Do Until lngIndex = Len(TESTDATA) + 1
For lngIndexNew = 1 To Len(strNew)
If Mid$(TESTDATA, lngIndex, 1) < Mid$(strNew, lngIndexNew, 1) Then
If lngIndexNew = 1 Then
' Add the new character at the beginning
strNew = Mid$(TESTDATA, lngIndex, 1) & strNew
Else
' Stick it in the middle somewhere
strNew = Left$(strNew, lngIndexNew - 1) & Mid$(TESTDATA, lngIndex, 1) & Mid$(strNew, lngIndexNew)
End If
lngIndex = lngIndex + 1
Exit For
End If
Next
Loop
-
Nov 4th, 2007, 09:28 PM
#4
Thread Starter
Addicted Member
Re: How to sort string?
Thank you for your code. I modify your code to function and test it.
vb Code:
Private Function SortString(ByVal strSource As String) As String
Dim strNew As String
Dim lngIndex As Long
Dim lngIndexNew As Long
strNew = Mid$(strSource, 1, 1)
lngIndex = 2
Do Until lngIndex = Len(strSource) + 1
For lngIndexNew = 1 To Len(strNew)
If Mid$(strSource, lngIndex, 1) < Mid$(strNew, lngIndexNew, 1) Then
If lngIndexNew = 1 Then
' Add the new character at the beginning
strNew = Mid$(strSource, lngIndex, 1) & strNew
Else
' Stick it in the middle somewhere
strNew = Left$(strNew, lngIndexNew - 1) & Mid$(strSource, lngIndex, 1) & Mid$(strNew, lngIndexNew)
End If
lngIndex = lngIndex + 1
Exit For
End If
Next
Loop
SortString = strNew
End Function
Private Sub Command1_Click()
MsgBox SortString("9874056213")
End Sub
If choose MsgBox SortString("9874056213") it worked.
If choose MsgBox SortString("3152") it is not worked.
-
Nov 4th, 2007, 10:04 PM
#5
Re: How to sort string?
Code:
'Const TESTDATA = "9874056213"
Const TESTDATA = "3152"
Dim strNew As String
Dim lngIndex As Long
Dim lngIndexNew As Long
Dim bInserted As Boolean
strNew = Mid$(TESTDATA, 1, 1)
lngIndex = 2
Do Until lngIndex = Len(TESTDATA) + 1
bInserted = False
For lngIndexNew = 1 To Len(strNew)
If Mid$(TESTDATA, lngIndex, 1) < Mid$(strNew, lngIndexNew, 1) Then
If lngIndexNew = 1 Then
' Add the new character at the beginning
strNew = Mid$(TESTDATA, lngIndex, 1) & strNew
bInserted = True
Else
' Stick it in the middle somewhere
strNew = Left$(strNew, lngIndexNew - 1) & Mid$(TESTDATA, lngIndex, 1) & Mid$(strNew, lngIndexNew)
bInserted = True
End If
lngIndex = lngIndex + 1
Exit For
End If
Next
If Not bInserted Then
strNew = strNew & Mid$(TESTDATA, lngIndex, 1)
lngIndex = lngIndex + 1
End If
Loop
-
Nov 4th, 2007, 10:12 PM
#6
Thread Starter
Addicted Member
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
|