I have an array of a structure. It's declared like this
Public SongsList as New List(Of Song)
"Song" it's structure's name. It has 2 variables : path and name; I'm wondering how I can sort this array by the name.
Public Structure Song
Public Path as String
Public Name as String
End Structure
I tried this
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
But it throws a NullReferenceException
.
This is how I'm adding items to 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