Sort an array of structures in .NET

前端 未结 7 1195
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 07:02

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 Nam         


        
相关标签:
7条回答
  • 2020-12-16 07:33
    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
    
    0 讨论(0)
提交回复
热议问题