【问题标题】:How can I sort an array of a structure?如何对结构数组进行排序?
【发布时间】:2015-09-25 23:24:55
【问题描述】:

我有一个结构数组。 它是这样声明的

Public SongsList as New List(Of Song)

“歌曲”是结构的名称。 它有 2 个变量:路径和名称; 我想知道如何按名称对这个数组进行排序。

Public Structure Song
  Public Path as String
  Public Name as String
End Structure

我试过了

ListBox1.Items.Clear()
Dim sorted = SongsList.OrderBy(Function(s) s.Name).ToList 
Dim i As Integer 
For i = 0 To sorted.Count - 1
    ListBox1.Items.Add(sorted(i).Name.ToString) 
Next

但它会抛出一个NullReferenceException

这就是我向 SongsList 添加项目的方式

Dim open As New OpenFileDialog
open.Title = "Add songs"
open.Filter = "MP3 Files(*.mp3)|*.mp3"
open.Multiselect = True
ListBox1.Items.Clear()
If open.ShowDialog = DialogResult.OK Then
    For Each SelectedSong As String In open.FileNames
        i += 1
        Dim songToAdd As New Song
        songToAdd.Path = SelectedSong.ToString
        songToAdd.Name = GetSafeFileName(SelectedSong.ToString)
        SongsList.Add(songToAdd)
        ListBox1.Items.Add(SongsList(i).Path)
    Next
End If

【问题讨论】:

  • 我编辑了您的问题以包含您在 OneFineDay 答案的 cmets 中提到的代码;但请下次尝试包含您尝试过的代码 - 即使它不起作用。现在,你能告诉我们你是如何填写SongsList的吗?
  • 为什么对已经是字符串的变量调用.ToString

标签: arrays vb.net list sorting


【解决方案1】:

您可以使用 Lambda 表达式。它使用您在OrderBy 函数中选择的字段。让我们重写ToString 方法来告诉列表框要显示什么,然后您可以将列表设置为数据源。

班级:

Public Class Song
  Public Property Path as String
  Public Property Name as String
  Public Overrides Function ToString() As String
    Return Me.Path
  End If
End Class

用法:

Dim open As New OpenFileDialog
open.Title = "Add songs"
open.Filter = "MP3 Files(*.mp3)|*.mp3"
open.Multiselect = True
If open.ShowDialog = DialogResult.OK Then
  For Each SelectedSong As String In open.FileNames
    Dim songToAdd As New Song
    songToAdd.Path = SelectedSong
    songToAdd.Name = GetSafeFileName(SelectedSong.ToString)
    SongsList.Add(songToAdd)
   Next
End If
Listbox1.DataSource = SongsList.OrderBy(Function(s) s.Name).ToList

【讨论】:

  • 我试过了,但没有用。这是代码: ListBox1.Items.Clear() Dim sorted = SongsList.OrderBy(Function(s) s.SongName).ToList Dim i As Integer For i = 0 To sorted.Count - 1 ListBox1.Items.Add(sorted (i).SongName.ToString) 下一个
  • NullReferenceException
  • 您在上面写道,结构中是Name,而不是SongName。是哪一个?
  • 没关系。我已经重写了代码,但我忘记了。对不起。使用“名称”。
  • 是的。每次我运行程序都会清除的列表。
【解决方案2】:

*与 OneFineDay 的回答非常相似...

您不需要自定义类,只需使用List(Of FileInfo)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim open As New OpenFileDialog
    open.Title = "Add songs"
    open.Filter = "MP3 Files(*.mp3)|*.mp3"
    open.Multiselect = True
    If open.ShowDialog = DialogResult.OK Then
        ListBox1.DataSource = Nothing
        Dim songs As New List(Of FileInfo)
        For Each SelectedSong As String In open.FileNames
            songs.Add(New FileInfo(SelectedSong))
        Next
        songs = songs.OrderBy(Function(fi) fi.Name).ToList
        ListBox1.DataSource = songs
        ListBox1.DisplayMember = "Name"
        ListBox1.ValueMember = "FullName"
    End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If ListBox1.SelectedIndex <> -1 Then
        Dim fi As FileInfo = DirectCast(ListBox1.SelectedItem, FileInfo)
        Dim name As String = fi.Name
        Dim fullPath As String = fi.FullName
        Debug.Print("name = " & name)
        Debug.Print("fullPath = " & fullPath)
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 2021-04-07
    相关资源
    最近更新 更多