This is one of those times when only the hive mind can help - no amount of Google-fu can!
I have an array of structures:
程序代码: |
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
How do I sort the array on one variable / property of the structure?
程序代码: |
Dim cars() as stCar = {new stCar("ford",10), new stCar("honda",50)}
cars.sort("MPH")
how do I do this?
Assuming that the structure has a property called MPH:
程序代码: |
cars = cars.OrderBy(Function(c) c.MPH)
Note: the above code was auto-converted from the following c# code (in case it contains errors):
程序代码: |
cars = cars.OrderBy(c => c.MPH);
The easiest way to perform the sort would be to use LINQ to Objects.
程序代码: |
Dim q = From c In cars Order By c.MPH Select c
Another possibility, that doesn't use Linq but instead uses the .Net Array class' Sort method:
程序代码: |
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
I'm not sure if that's what you're looking for, but it's another approach.
A simple way that seems to be working for me in vb.net 2013 is as follows:
程序代码: |
cars.Sort(Function(c1,c2) c1.MPH.CompareTo(c2.MPH))
In VB.Net of VS2015 there is another method of sorting:
程序代码: |
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))
程序代码: |
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
[本日志由 tiancao1001 于 2019-07-13 01:05 AM 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |