|
'通用数组排序 - 自定义函数Array_Sort(支持二维或一维)---by 泓()
Function Array_Sort(Array_, Key1, Order) '(Array_[将要排序的数组], Key1[垂直数组(y,x)中x,像表格中的哪一列作关键字], Order[=1,升序;<>1,降序])
Dim t, x&, y&, i&, j&, k&, xx&, yy&, tt&, AD&
For i = 1 To 60
On Error Resume Next
Err.Clear
tt = UBound(Array_, i)
If Err.Number = 9 Then AD = i - 1: Exit For 'AD,数组维数
Next
If AD = 2 Then
If Not (Key1 >= LBound(Array_, 2) And Key1 <= UBound(Array_, 2)) Then Exit Function
ElseIf AD = 1 Then
Array_ = Application.Transpose(Array_)
Key1 = 1
Else
Exit Function
End If
y = LBound(Array_, 1): x = LBound(Array_, 2)
yy = UBound(Array_): xx = UBound(Array_, 2)
If Order = 1 Then '升序
For i = y To yy - 1
For j = i + 1 To yy
If Array_(j, Key1) < Array_(i, Key1) Then '冒泡排序法
For k = x To xx
t = Array_(j, k): Array_(j, k) = Array_(i, k): Array_(i, k) = t
Next
End If
Next
Next
Else '降序
For i = y To yy - 1
For j = i + 1 To yy
If Array_(j, Key1) > Array_(i, Key1) Then
For k = x To xx
t = Array_(j, k): Array_(j, k) = Array_(i, k): Array_(i, k) = t
Next
End If
Next
Next
End If
If AD = 2 Then Array_Sort = Array_ Else Array_Sort = Application.Transpose(Array_)
End Function
修改版
通用数组排序 - 自定义函数(支持二维或一维).rar
(18.69 KB, 下载次数: 548)
|
评分
-
2
查看全部评分
-
|