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

时间:2009-11-17 21:04:33

标签: vb.net data-structures sorting

这是只有蜂巢头脑才能提供帮助的时代之一 - 没有多少谷歌可以帮助!

我有一系列结构:

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 个答案:

答案 0 :(得分:14)

假设结构具有称为MPH的属性:

cars = cars.OrderBy(Function(c) c.MPH)

注意:上面的代码是从以下c#代码中自动转换的(如果它包含错误):

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

答案 1 :(得分:4)

执行排序的最简单方法是使用LINQ to Objects。

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

答案 2 :(得分:2)

我不知道VB.NET,但你应该能够做到这一点我的恭维IComparer。看一下这个例子

http://www.java2s.com/Code/VB/Data-Structure/UseIComparertosortbydifferentproperties.htm

另外,您也可以使用Linq

答案 3 :(得分: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

我不确定这是不是你想要的,但这是另一种方法。

答案 4 :(得分:1)

在vb.net 2013中,似乎对我有用的一种简单方法如下:

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

答案 5 :(得分:0)

在VS2015的VB.Net中,还有另一种排序方法:

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))

答案 6 :(得分:-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