在.NET中对结构数组进行排序

6

这是那种只有群体智慧才能帮助的时候 - 无论你怎么谷歌都找不到答案!

我有一个结构数组:

Structure stCar 
    Dim Name As String
    Dim MPH As Integer

    Sub New(ByVal _Name As String, ByVal _MPH As Integer)
        Name = _Name
        MPH = _MPH
    End Sub
End Structure

我该如何对结构体的一个变量/属性进行排序?
Dim cars() as stCar = {new stCar("ford",10), new stCar("honda",50)}

cars.sort("MPH") // how do I do this?
7个回答

15
假设该结构体具有名为MPH的属性:
cars = cars.OrderBy(Function(c) c.MPH)

注意:上述代码是从以下 C# 代码自动转换而来(如果包含错误):

cars = cars.OrderBy(c => c.MPH);

2
这是我用过的有效方法: cars = cars.OrderBy(Function(c) c.MPH).ToArray - Rob

5

最简单的排序方式是使用LINQ to Objects。

Dim q = From c In cars Order By c.MPH Select c

2

1
你不能在结构体上实现IComparer。我认为它必须针对类进行实现,而在VB.NET中结构体不是类。 - Nick DeVore

1

在vb.net 2013中,似乎有一种简单的方法可以运行,具体步骤如下:

cars.Sort(Function(c1,c2) c1.MPH.CompareTo(c2.MPH))

1

另一种可能性是,不使用Linq,而是使用.Net Array类的Sort方法:

Module Module1
    Structure stCar
        Dim Name As String
        Dim MPH As String

        Sub New(ByVal _Name As String, ByVal _MPH As Integer)
            Name = _Name
            MPH = _MPH
        End Sub
    End Structure

    Class CarCompareMph : Implements IComparer

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            Dim xCar As stCar = DirectCast(x, stCar)
            Dim yCar As stCar = DirectCast(y, stCar)
            Return New CaseInsensitiveComparer().Compare(xCar.MPH, yCar.MPH)
        End Function
    End Class

    Sub Main()
        Dim cars() As stCar = {New stCar("honda", 50), New stCar("ford", 10)}
        Array.Sort(cars, New CarCompareMph)

        For Each c As stCar In cars
            Console.WriteLine("{0} - {1} MPH", c.Name, c.MPH)
        Next
    End Sub

End Module

我不确定那是否是你正在寻找的,但这是另一种方法。


将字典排序应用于数字值的集合不会达到您想要的效果。它可能适用于大多数数据集,但当您的程序将兰博基尼放在普锐斯前面时,您会感到惊讶["8">"48"]。 - bmm6o

0
在VB.Net的VS2015中,还有另一种排序方法:
cars.Sort(New Comparison(Of stCar)(Function(x, y) 'x and y are of type of stCar
                                                             If x.MPH > y.MPH Then
                                                                 Return 1 ' Return Value>0 means x>y
                                                             End If
                                                             If x.MPH < y.MPH Then
                                                                 Return -1 ' Return Value<0 means x<y
                                                             End If
                                                             If x.MPH = y.MPH Then
                                                                 Return 0 ' Return Value=0 means x=y
                                                             End If
                                                         End Function))

-1
Public Class Form1
    Public Structure EstruturaPessoa
        Dim nome As String
        Dim idade As Integer
    End Structure

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim nome(,) As String = {{"Milton Inácio Pozza", 30}, _
                                 {"Araci Moraes", 34}, _
                                 {"Marli Lipi Jesus", 21}, _
                                 {"Gerson Guebur", 45}, _
                                 {"Marli Ulths", 72}, _
                                 {"Mauro Jesus Nadolni", 56}, _
                                 {"Cristiano Kobashikawa Ferreira", 44}, _
                                 {"Débora Nadolni", 90}, _
                                 {"Samanta Gomes Guebur", 66}, _
                                 {"Miguel Barbosa", 42}, _
                                 {"Luis Jesus", 24} _
                                }
        Dim Pessoa As EstruturaPessoa
        Dim ListaPessoa = New List(Of EstruturaPessoa)
        Dim i As Integer

        'Load ListaPessoa
        For i = 0 To (nome.Length / 2) - 1
            Pessoa.nome = nome(i, 0)
            Pessoa.idade = nome(i, 1)
            ListaPessoa.Add(Pessoa)
        Next i

        'Fill the ListView1 with the ListaPessoa before sorting
        For Each item_pessoa In ListaPessoa
            With Me.ListView1
                .Items.Add(item_pessoa.nome)
                With .Items(.Items.Count - 1).SubItems
                    .Add(item_pessoa.idade)
                End With
            End With
        Next

        'Sort ListaPessoa
        ListaPessoa.Sort(Function(c1, c2) c1.nome.CompareTo(c2.nome))

        'Modifiy the 6th item of ListaPessoa
        Pessoa = ListaPessoa(5)
        Pessoa.nome += " ***"   'Acrescenta asteriscos ao nome
        Pessoa.idade = 99       'Modifica a idade para 99
        ListaPessoa(5) = Pessoa 'Atualiza o item na ListaPessoa

        'Fill ListView2 with the ListaPessoa after sorting
        For Each item_pessoa In ListaPessoa
            With Me.ListView2
                .Items.Add(item_pessoa.nome)
                With .Items(.Items.Count - 1).SubItems
                    .Add(item_pessoa.idade)
                End With
            End With
        Next
    End Sub
End Class

2
欢迎来到 Stack Overflow!Stack Overflow 通常使用英语,请确保您的帖子尽可能地对其他人有用,可以翻译评论和变量名称。 - Nathan Tuggy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接