如何在Visual Basic中对List(Of Class)类型的对象列表进行冒泡排序?
实现List(Of Exercises)的冒泡排序(Visual Basic)
需求说明
需要基于冒泡排序逻辑,实现对List(Of Exercises)对象列表的排序,支持按指定字段升序(ASC)或降序(DESC)排列。已定义Exercises类并声明对象列表,现有排序代码未完成,需补充完整实现。
现有代码
对象列表声明
Dim Exercises As New List(Of Exercises)
Exercises类定义
Public Class Exercises 'properties Private _Name As String Private _Position As String Private _Equipment As String Private _bodyPart As String Private _beginnerStandard As Integer Private _noviceStandard As Integer Private _intermediateStandard As Integer Private _advancedStandard As Integer Private _eliteStandard As Integer Public Sub New() 'default constructor Name = "" Position = "" Equipment = "" bodyPart = "" beginnerStandard = 0 noviceStandard = 0 intermediateStandard = 0 advancedStandard = 0 eliteStandard = 0 '修正原代码重复赋值advancedStandard的错误 End Sub Public Sub New(ByVal Name As String, ByVal Position As String, ByVal Equipment As String, ByVal bodypart As String, ByVal beginnerStandard As Integer, ByVal noviceStandard As Integer, ByVal intermediateStandard As Integer, ByVal advancedStandard As Integer, ByVal eliteStandard As Integer) 'overloaded constructor _Name = Name _Position = Position _Equipment = Equipment _bodyPart = bodypart _beginnerStandard = beginnerStandard _noviceStandard = noviceStandard _intermediateStandard = intermediateStandard _advancedStandard = advancedStandard _eliteStandard = eliteStandard End Sub 'getters and setters for the properties Public Property Name As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property Public Property Position As String Get Return _Position End Get Set(ByVal value As String) _Position = value End Set End Property Public Property Equipment As String Get Return _Equipment End Get Set(ByVal value As String) _Equipment = value End Set End Property Public Property bodyPart As String Get Return _bodyPart End Get Set(ByVal value As String) _bodyPart = value End Set End Property Public Property beginnerStandard As Integer Get Return _beginnerStandard End Get Set(ByVal value As Integer) _beginnerStandard = value End Set End Property Public Property noviceStandard As Integer Get Return _noviceStandard End Get Set(ByVal value As Integer) _noviceStandard = value End Set End Property Public Property intermediateStandard As Integer Get Return _intermediateStandard End Get Set(ByVal value As Integer) _intermediateStandard = value End Set End Property Public Property advancedStandard As Integer Get Return _advancedStandard End Get Set(ByVal value As Integer) _advancedStandard = value End Set End Property Public Property eliteStandard As Integer Get Return _eliteStandard End Get Set(ByVal value As Integer) _eliteStandard = value End Set End Property End Class
未完成的排序代码
Sub bubbleSort(ByVal OrderAscDesc, ByVal orderColumn) Dim tempHolder As Exercises Dim outer As Integer = getArrayOfObjectsLength() Dim noSwap As Boolean = True If OrderAscDesc = "ASC" Then While Not (outer = -1) Or noSwap = False noSwap = True For Each obj In Exercises If obj. End If Next End While ElseIf OrderAscDesc = "DESC" Then End If End Sub '原函数存在逻辑错误,无法正确获取列表长度 Function getArrayOfObjectsLength() Dim elements = 0 For Each obj In Exercises elements += Exercises.Name.Length Next Dim newArray(elements - 1) As Integer Dim index = 0 For Each obj In Exercises Dim n = Exercises.Result.Length Array.Copy(Exercises.Result, 0, newArray, index, n) index += n Next Console.WriteLine($"The new array has {newArray.Length} elements.") End Function
完整实现方案
修正后的排序代码
'按指定字段和顺序冒泡排序List(Of Exercises) Sub bubbleSort(ByVal OrderAscDesc As String, ByVal orderColumn As String) Dim tempHolder As Exercises Dim listCount As Integer = Exercises.Count Dim noSwap As Boolean '冒泡排序核心逻辑 For outer As Integer = listCount - 1 To 1 Step -1 noSwap = True For inner As Integer = 0 To outer - 1 '根据排序字段和顺序判断是否需要交换 If NeedSwap(Exercises(inner), Exercises(inner + 1), orderColumn, OrderAscDesc) Then '交换两个对象 tempHolder = Exercises(inner) Exercises(inner) = Exercises(inner + 1) Exercises(inner + 1) = tempHolder noSwap = False End If Next '如果本轮无交换,说明列表已排序,提前退出 If noSwap Then Exit For Next End Sub '判断两个Exercises对象是否需要交换的辅助函数 Private Function NeedSwap(ByVal obj1 As Exercises, ByVal obj2 As Exercises, ByVal column As String, ByVal sortOrder As String) As Boolean Select Case column.ToLower() Case "name", "position", "equipment", "bodypart" '字符串类型字段比较,使用文本比较(忽略大小写) Dim compareResult As Integer = String.Compare(obj1.GetType().GetProperty(column).GetValue(obj1).ToString(), obj2.GetType().GetProperty(column).GetValue(obj2).ToString(), StringComparison.OrdinalIgnoreCase) Return If(sortOrder = "ASC", compareResult > 0, compareResult < 0) Case "beginnerstandard", "novicestandard", "intermediatestandard", "advancedstandard", "elitestandard" '数值类型字段比较 Dim val1 As Integer = CInt(obj1.GetType().GetProperty(column).GetValue(obj1)) Dim val2 As Integer = CInt(obj2.GetType().GetProperty(column).GetValue(obj2)) Return If(sortOrder = "ASC", val1 > val2, val1 < val2) Case Else '无效字段,默认不交换 Return False End Select End Function
关键说明
- 修正原代码错误:移除了无效的
getArrayOfObjectsLength函数,直接使用List(Of T).Count获取列表长度,更高效准确。 - 通用字段支持:通过反射获取指定字段的值,支持所有
Exercises类的属性作为排序依据,无需为每个字段单独写判断逻辑。 - 类型区分比较:针对字符串类型(如Name、Position)使用不区分大小写的文本比较,数值类型(如各等级标准)直接进行数值比较。
- 提前终止优化:如果某一轮冒泡没有发生任何交换,说明列表已经有序,直接退出循环,减少不必要的遍历。
- 参数类型明确:为
bubbleSort方法的参数指定了类型(String),符合VB的类型安全要求。
使用示例
'示例:按Name字段升序排序 bubbleSort("ASC", "Name") '示例:按beginnerStandard字段降序排序 bubbleSort("DESC", "beginnerStandard")
内容的提问来源于stack exchange,提问作者Manik Khadiya
相关产品推荐
相关产品推荐

