Function OrderStr(str As String) As String
Dim s As String
Dim j As Long
Dim n As Long
s = Mid(str, 1, 1)
For j = 1 To Len(str)
If Mid(str, j, 1) < s Then s = Mid(str, j, 1): n = j '计算最小值及其位置。
Next
str = s & Replace(str, s, "", 1, 1) '排序
OrderStr = str
End Function
第二步 做第一位到倒数第二位的循环比较排序。函数可写为:
Function OrderStr(str As String) As String
Dim s As String
Dim i As Long, j As Long
Dim n As Long
For i = 1 To Len(str) - 1
s = Mid(str, i, 1)
For j = i + 1 To Len(str)
If Mid(str, j, 1) < s Then s = Mid(str, j, 1): n = j
Next
str = Mid(str, 1, i - 1) & s & Replace(str, s, "", i, 1)
Next
OrderStr = str
End Function
第三步 做可选择顺序和逆序的排序函数
Function OrderStr(str As String, B As Long) As String
'功能:字符串排序
'参数:str--源字符串,B=1 降序 B=2 升序
Dim s As String
Dim i As Long, j As Long
Dim n As Long
For i = 1 To Len(str) - 1
s = Mid(str, i, 1)
For j = i + 1 To Len(str)
Select Case B
Case 1
If Mid(str, j, 1) < s Then s = Mid(str, j, 1): n = j
Case 2
If Mid(str, j, 1) > s Then s = Mid(str, j, 1): n = j
End Select
Next
str = Mid(str, 1, i - 1) & s & Replace(str, s, "", i, 1)
Next
OrderStr = str
End Function