在vb.net中对类数组进行排序 [英] To sort a class array in vb.net
问题描述
我花了2天的时间在互联网上进行搜索,试图找到一种解决方案,以简单地对由一类字符串和整数组成的数组进行排序(仅由可能包含不规则字符的字符串元素中的1个组成).请帮忙!我已经基于Microsoft示例创建了我想做的简化代码:
I have spent 2 days scouring the internet trying to find the solution to simply sort an array made up of a class of strings and integers (by just 1 of the string elements that may contain irregular characters). Please help! I have created a simplified code of what I am trying to do based on the microsoft example:
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
推荐答案
If you want to sort by the Make
property one out-of-the-box approach is using Array.Sort
with the Comparison(Of T)
overload:
Array.Sort(arrayOfCars, Function(car1 As Car, car2 as Car)
Return car1.Make.CompareTo(car2.Make)
End Function)
请注意,您应该注意Nothing
或(更可能是)Make
-Nothing
值的汽车.两者都将导致NullReferenceException
.因此,您可以使用:
Note that you should take care of cars which are Nothing
or (more likely) Make
-values which are Nothing
. Both would cause a NullReferenceException
. Therefore you could use:
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:
Another (little less efficient since it needs to recreate the array) approach is LINQ:
Dim orderedCars = from car in arrayOfCars
order by car.Make Ascending
arrayOfCars = arrayOfCars.ToArray()
LINQ方法更易于维护,更易于阅读,但需要创建一个新的数组.因此,如果您不想修改原始数组,则应使用该数组.
The LINQ approach is more maintainable and easier to read but it needs to create a new array. So if you don't want to modify the original array you should use that.
通常,如果要添加对象,则不应该使用数组,因为数组是固定大小的集合.使用List(Of Car)
代替,它具有
In general you should not use arrays if you want to add objects since arrays are fixed-sized collections. Use a List(Of Car)
instead, it has an Add
method.
另一个nitpick,请按照.NET命名/大写约定,请使用Car
代替car
.
Another nitpick, follow .NET naming/capitalization conventions, use Car
instead of car
.
这篇关于在vb.net中对类数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!