我花了两天时间在互联网上搜索,试图找到解决方案来简单地对由一类字符串和整数组成的数组进行排序(仅包含一个可能包含不规则字符的字符串元素)。请帮忙!我已经根据microsoft示例创建了一个简化的代码:
Public Class Form1
Class car
Public Make As String = ""
Public Year As Integer = 0
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayOfCars() As car
Dim arrayElement As Integer = 0
'Exploded simplified loop to fill the array (original has 20 objects in the class
'and the array grows depending on input to no more than a few hundred.
ReDim arrayOfCars(0)
arrayOfCars(0) = New car
arrayOfCars(0).Make = "Ford"
arrayOfCars(0).Year = 1992
ReDim Preserve arrayOfCars(1)
arrayOfCars(1) = New car
arrayOfCars(1).Make = "Fiat"
arrayOfCars(1).Year = 1988
ReDim Preserve arrayOfCars(2)
arrayOfCars(2) = New car
arrayOfCars(2).Make = "Buick"
arrayOfCars(2).Year = 1932
ReDim Preserve arrayOfCars(3)
arrayOfCars(3) = New car
arrayOfCars(3).Make = "Ford"
arrayOfCars(3).Year = 1932
ReDim Preserve arrayOfCars(4)
arrayOfCars(4) = New car
arrayOfCars(4).Make = "Dodge"
arrayOfCars(4).Year = 1999
ReDim Preserve arrayOfCars(5)
arrayOfCars(5) = New car
arrayOfCars(5).Make = "Honda"
arrayOfCars(5).Year = 1977
'view array before sort
For i = 0 To 5
Debug.WriteLine(arrayOfCars(i).Make & vbTab & arrayOfCars(i).Year)
Next
Debug.WriteLine("*************************")
'sort array by the string component [Make]
'Array.Sort(arrayOfCars)
'arrayOfCars = arrayOfCars.OrderBy(Function(car) car.Make)
'????????????????
'view array after sort
For i = 0 To 5
Debug.WriteLine(arrayOfCars(i).Make & vbTab & arrayOfCars(i).Year)
Next
End Sub
End Class
答案 0 :(得分:2)
如果您想按Make
属性进行排序,则一种开箱即用的方法是Array.Sort
使用Comparison(Of T)
重载:
Array.Sort(arrayOfCars, Function(car1 As Car, car2 as Car)
Return car1.Make.CompareTo(car2.Make)
End Function)
请注意,您应该处理Nothing
或(更有可能)Make
- Nothing
的值的汽车。两者都会导致NullReferenceException
。因此,您可以使用:
Array.Sort(arrayOfCars, Function(car1 As Car, car2 As Car)
If Object.ReferenceEquals(car1,car2)
return 0
Else if car1 is nothing
Return -1
Else if car2 is nothing
Return 1
Else
return String.Compare(car1.Make, car2.Make)
End If
End Function)
另一个(因为它需要重新创建数组的效率稍差)方法是LINQ:
Dim orderedCars = from car in arrayOfCars
order by car.Make Ascending
arrayOfCars = arrayOfCars.ToArray()
LINQ方法更易于维护且更易于阅读,但需要创建新阵列。因此,如果您不想修改原始数组,则应使用该数组。
通常,如果要添加对象,则不应使用数组,因为数组是固定大小的集合。请改用List(Of Car)
,它有一个Add
method。
另一个挑剔,请遵循.NET命名/ capitalization conventions,使用Car
代替car
。
答案 1 :(得分:1)
Linq是你的朋友和OrderBy
方法。但是为了使用你的数组需要List
。
您可以轻松地将其转换为List并按照这样的make对其进行排序(不是这不会对原始列表进行排序,而是创建一个按您指定的条件排序的新列表):
Dim sortedListOfCars = arrayOfCars.ToList.OrderBy(Function(x) x.Make)
但是将它声明为一个开头的列表会更容易:
Dim listOfCars As New List(Of Car)
然后像这样添加你的车
listOfCars.Add(New Car With {.Make = "Ford", .Year = 1992})
listOfCars.Add(New Car With {.Make = "Fiat", .Year = 1988})
'etc.
然后你直接订购OrderBy:
Dim sortedListOfCars = arrayOfCars.OrderBy(Function(x) x.Make)
答案 2 :(得分:0)
您可以为Array.Sort()函数实现IComparer。
Public Class CarComparer : Implements IComparer
Function Compare(x As car, y As car) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(x.Make, y.Make)
End Function
End Class
像这样使用:
Array.Sort(arrayOfCars, new CarComparer())