2009-11-17 55 views
4

這是那些只有蜂巢心靈可以提供幫助的時代之一 - 沒有任何Google-fu可以!在.NET中對結構數組進行排序

我有一個結構數組:

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? 

回答

10

假定結構具有屬性調用MPH:

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

注意:從下面的C#代碼上面的代碼被自動轉換(如果它包含錯誤):

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

這是對我有用的東西:cars = cars.OrderBy(Function(c)c.MPH).ToArray – Rob 2017-01-08 02:56:24

4

最簡單的方法來執行排序是使用LINQ到對象。

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

另一種可能性,即不使用Linq而是使用.NET Array類排序方法:

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 

我不知道,如果這就是你要找的東西,但這是另一種方法。

+0

將一個詞典排序順序應用於數值集合不會達到您想要的效果。它可能適用於大多數數據集,但當您的程序將蘭博基尼提前於Prius [「8」>「48」]時,您會感到驚訝。 – bmm6o 2009-11-17 22:21:03

+0

這對我有用。漂亮整潔的解決方案。歡呼鮑勃 – FrinkTheBrave 2013-03-02 09:51:59

1

,似乎在vb.net 2013年是爲我工作的一個簡單方法如下:

cars.Sort(Function(c1,c2) c1.MPH.CompareTo(c2.MPH)) 
-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通常用英文](http://blog.stackoverflow.com/2009/07/non-english-question-policy/);請通過翻譯評論以及可能的變量名稱來確保您的帖子對其他人有用。 – 2015-03-20 00:13:23

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