我使用了一个冒泡排序函数,根据对象的属性对其进行排序。不幸的是,我不得不为每个属性编写一个函数。有什么方法可以概括下面的函数吗。
而不是写
SortArticlesByVolume(a)
SortArticlesByLenght(a)
我想写一些类似的东西:
SortArticles(a, volume)
按数量分类文章(按引用文章作为变体)
Dim sorted As Boolean
Dim i As Integer
sorted = False
Do While Not sorted
sorted = True
For i = 0 To UBound(articles) - 1
If articles(i).volume < articles(i + 1).volume Then
Set temp = articles(i + 1)
Set articles(i + 1) = articles(i)
Set articles(i) = temp
sorted = False
End If
Next i
Loop
结束子
Sub Sorts articles Byright(ByRef articles As Variant)
Dim sorted As Boolean
Dim i As Integer
sorted = False
Do While Not sorted
sorted = True
For i = 0 To UBound(articles) - 1
If articles(i).lenght < articles(i + 1).lenght Then
Set temp = articles(i + 1)
Set articles(i + 1) = articles(i)
Set articles(i) = temp
sorted = False
End If
Next i
Loop
结束子
我认为VBA不允许您使用反射,所以我认为您需要重写文章类,并添加按名称获取属性的方法,例如article.get("volume")
来获取卷。如果你没有太多的字段,这可能是最简单的方法。get函数仍然需要某种Select或If/Then语句,但它将封装在类本身中。