【发布时间】:2015-05-26 16:01:28
【问题描述】:
我在正确地根据包含数字的字符串对数组进行数字排序时遇到了一些问题:
因此,排序适用于所有字符串,直到我输入 2 位数字。
例如,如果数组包含“问题 2:”、“问题 5:”和“问题 3:”,它会将字符串正确排序为 2、3、5。
不幸的是,一旦我得到两位数字,它就不再正确排序。所以 "Issue 10:" "Issue 8:" 和 "Issue 13:" 不会排序。
我相当肯定这与我尝试基于字符串而不是数值排序这一事实有关。有没有办法让它通过字符串正确排序?或者是否有一种“简单”的方法可以将字符串数字更改为实际数值。
'This creates a list of what we want to sort by.
'The string format will always be "Issue 1:" "Issue 3:" "Issue 2:" "Issue 11:"
'Issue x:" etc.
IssueListActual = CreateIssueListFromSection(sectionFind)
'This creates a duplicate array to be sorted
IssueListSorted = IssueListActual
'Sorts the array as seen in below subroutine
BubbleSort IssueListSorted
Sub BubbleSort(arr)
Dim strTemp As String
Dim i As Long
Dim j As Long
Dim lngMin As Long
Dim lngMax As Long
lngMin = LBound(arr)
lngMax = UBound(arr)
For i = lngMin To lngMax - 1
For j = i + 1 To lngMax
If arr(i) > arr(j) Then
strTemp = arr(i)
arr(i) = arr(j)
arr(j) = strTemp
End If
Next j
Next i
End Sub
【问题讨论】: