我想用VBA按字母顺序对二维数组memo[]
进行排序。此数组包含4列和可变行数,基于最后一列的值。
这是我填充数组的代码:
results(lcol, 4)
答案 0 :(得分:0)
这在填充的数组上提供了一个基本的(quiksort?)升序排序,最后一列作为主键。
dim i as long, j as long, tmp as variant
redim tmp(lbound(results, 1) to lbound(results, 1), lbound(results, 2) to ubound(results, 2))
for i = lbound(results, 1) to ubound(results, 1) - 1
if results(i, ubound(results, 2)) > results(i+1, ubound(results, 2)) or _
results(i, ubound(results, 2)) = vbnullstring then
for j = lbound(results, 2) to ubound(results, 2)
tmp(lbound(results, 1), j) = results(i, j)
next j
for j = lbound(results, 2) to ubound(results, 2)
results(i, j) = results(i+1, j)
next j
for j = lbound(results, 2) to ubound(results, 2)
results(i+1, j) = tmp(lbound(results, 1), j)
next j
end if
next i
对不起所有的lbound和ubound,但我不知道你的数组是否从零开始是基于1的。 For i = 1 To lcol
不是确定的。所有证据都表明你的arr是从零开始的。
答案 1 :(得分:0)
你可以让SortedList对象完成工作
假设您的结果数组是基于1并且有4列,您可以尝试以下代码(UNTESTED):
Sub SortArray(results As Variant)
Dim i As Long, j As Long
With CreateObject("System.Collections.SortedList")
For i = 1 to UBound(results)
.Add results(i,4), Application.Index(result,i,0)
Next
For i = 1 To .Count
For j = 1 To 4
results(i, j) = .GetByIndex(i)(j)
Next
Next
End With
End Sub
您可以在“主要”子系统中调用,如下所示:
SortArray results