我正在尝试对自定义类进行排序,代码没有错误,但是没有按要求按Coulmn1对列表进行排序?
Class MainWindow
Private Rows As New List(Of Row)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Rows.Add(New Row With {.Column1 = "Z", .Column2 = 2})
Rows.Add(New Row With {.Column1 = "D", .Column2 = 1})
Rows.Add(New Row With {.Column1 = "S", .Column2 = 2})
Rows.Add(New Row With {.Column1 = "A", .Column2 = 1})
Rows.Sort(Function(x As Row, y As Row) x.Column1.CompareTo(y.Column1))
End Sub
End Class
Public Class Row
Public Property Column1 As String
Public Property Column2 As Int32
End Class
答案 0 :(得分:-1)
希望这会有所帮助。
此人使用 OrderBy 。
Class MainWindow
Private Rows As New List(Of Row)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Rows.Add(New Row With {.Column1 = "Z", .Column2 = 2})
Rows.Add(New Row With {.Column1 = "D", .Column2 = 1})
Rows.Add(New Row With {.Column1 = "S", .Column2 = 2})
Rows.Add(New Row With {.Column1 = "A", .Column2 = 1})
Rows = Rows.OrderBy(Function(x) x.Column1).ToList()
Rows = Rows.OrderBy(Function(y) y.Column1).ToList()
End Sub
End Class
Public Class Row
Public Property Column1 As String
Public Property Column2 As Int32
End Class
然后使用排序。
Class MainWindow
Private Rows As New List(Of Row)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Rows.Add(New Row With {.Column1 = "Z", .Column2 = 2})
Rows.Add(New Row With {.Column1 = "D", .Column2 = 1})
Rows.Add(New Row With {.Column1 = "S", .Column2 = 2})
Rows.Add(New Row With {.Column1 = "A", .Column2 = 1})
Rows.Sort(Function(x As Row, y As Row) x.Column1.CompareTo(y.Column1))
Rows.Sort(Function(x As Row, y As Row) x.Column2.CompareTo(y.Column2))
End Sub
End Class
Public Class Row
Public Property Column1 As String
Public Property Column2 As Int32
End Class