田草博客

互联网田草博客


网友交流QQ群:11740834 需注明申请加入原因

微信 公众号:ByCAD

邮箱:tiancao1001x126.com
ByCAD,微信公众号
首页 | 普通 | 电脑 | AutoCAD | VB/VB.NET | FLash | 结构 | 建筑 | 电影 | BIM | 规范 | 软件 | ID
-随机-|-分布-
-博客论坛-|-﨣﨤﨧﨨-
-网站导航-|-规范下载-
-BelovedFLash欣赏-

用户登陆
用户:
密码:
 

站点日历
73 2024 - 3 48
     12
3456789
10111213141516
17181920212223
24252627282930
31


站点统计

最新评论



Structure 类型排序 New ArrayList
未知 Sort an array of structures in .NET   [ 日期:2019-07-13 ]   [ 来自:本站原创 ]  HTML
https://stackoverflow.com/questions/175 ... ay-of-structures-in-net

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 编辑]


暂时没有评论
发表评论 - 不要忘了输入验证码哦!
作者: 用户:  密码:   注册? 验证:  防止恶意留言请输入问题答案:2*2=?  
评论:

禁止表情
禁止UBB
禁止图片
识别链接
识别关键字

字体样式 文字大小 文字颜色
插入粗体文本 插入斜体文本 插入下划线
左对齐 居中对齐 右对齐
插入超级链接 插入邮件地址 插入图像
插入 Flash 插入代码 插入引用
插入列表 插入音频文件 插入视频文件
插入缩进符合
点击下载按钮 下标 上标
水平线 简介分割标记
表  情
 
Tiancao Blog All Rights Reserved 田草博客 版权所有
Copyright ©