您只需要创建一个类。我们还可以利用 .Net 的内置数据结构之一,即SortedList。为了使用排序列表,您的类需要实现iComparable,我将在下面介绍。
Class Product
public Item as string
public Descrip as string
public Price as decimal
public Bin as string
end class
现在,你的类需要实现iComparable
我们将修改类如下
Class Product
Implements IComparable
public Item as string
public Descrip as string
public Price as decimal
public Bin as string
Public Overloads Function CompareTo(ByVal obj As Object) As Integer
if obj is nothing then return 1
Dim otherObj As Product = TryCast(obj, Product)
If otherObj IsNot Nothing Then
if me.bin < otherObj.bin then
return -1
else if me.bin = otherObj.bin andalso me.item < otherObj.item then
return -1
elseif me.bin = otherObj.bin andalso me.item = otherObj.item then
return 0
else
return 1
Else
Throw New ArgumentException("Object is not a Product")
End If
End Function
end class
现在,您应该使用SortedList(of Product)
你像这样向它添加元素
Dim MyProduct as NewProduct
MyProduct.bin = "test"
MyProduct.price = 12.0D
MyProduct.item = "test"
MyProduct.descrip = "test"
Dim MySortedList = new SortedList(of Product)
MySortedList.add(NewProduct)
MySortedList 始终保持其元素有序。
上面的代码可以优化一些,但我想你明白了。
参考