只有当对象的某些属性可以比较以确定哪个被认为较小,哪个被认为较大时,才能自动进行排序。这可以通过数字、日期、文本和任何其他实现
IComparable
和/或
IComparable(Of T)
接口。如果没有这样的属性,那么您仍然可以在
Comparison(Of T)
委托或实现的类
IComparer
IComparer(Of T)
. 请看我的三篇博文
here
更多细节。
作为一个例子,下面是如何使用
比较(T)
代理内联:
Dim files As List(Of FileInfo)
'...
files.Sort(Function(fi1, fi2)
Dim fileNames = {"The First File.pdf",
"The Second File.pdf",
"The Third File.pdf",
"The Fourth File.pdf"}
Return Array.IndexOf(fileNames, fi1.Name).CompareTo(Array.IndexOf(fileNames, fi2.Name))
End Function)
(这被认为是实施
排序次序
Compare