如何对一个成员(整数)的列表(T)进行排序,其中 T 是自定义数据类?
Public Class CustomObject
Public Property text1 as String
Public Property counter1 as Integer
Public Property counter2 as Integer
End Class
Public Objectlist As New List(Of CustomObject)
.add, .add, .add etc.
Objectlist.sort(???...)
最佳答案
您可以使用 Sort overload which takes a predicate 来执行此操作:
ObjectList.Sort(Function(i,j) i.counter1.CompareTo(j.counter1))
请注意,您还可以使用 LINQ 返回新对象:
Dim sorted = ObjectList.OrderBy(Function(i) i.counter1)
如果您需要降序排序,您可以这样做:
ObjectList.Sort(Function(i,j) j.counter1.CompareTo(i.counter1))
或者:
Dim sorted = ObjectList.OrderByDescending(Function(i) i.counter1)
关于vb.net - List(of T) 对自定义类的成员进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12919715/