1

I don't know a lot about the subject of sorting but here goes: I am trying to sort a music library (comma seperated in a csv file. Some examples):

1,Sweet Home Alabame,Lynyrd Skynyrd,4:40,Classic Rock

2,Misirlou,Dick Dale,2:16,Surf Rock

I need to sort them alphabetically (by title of track) but I don't know two things: 1. Why my current technique isn't working:

Dim array() As String = {}

sr = New StreamReader("library.csv")
counter = 1
Do Until sr.EndOfStream
    array(counter) = sr.ReadLine()
    counter += 1
Loop

System.Array.Sort(Of String)(array)

Dim value As String
For Each value In array
    Console.WriteLine(value)
Next

Console.ReadLine()

I don't know if this is the best way of sorting. I then need to display them as well. I can do this without sorting, but can't figure out how to do it with sorting.

Help please (from people who, unlike me, know what they're doing).

InteXX
  • 6,135
  • 6
  • 43
  • 80
Jay Cloth
  • 11
  • 1

1 Answers1

1

Right now you're putting all fields in one long string of text (each row).

In order to sort by a particular field, you'll need to build a matrix of rows and columns. For example, a DataTable.

Here's a class that should do the trick for you:

https://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F

Here's the sample usage code from the article, translated to VB:

Public Class CsvImporter
  Public Sub Import()
    Dim dsResult As DataSet

    ' Using an XML Config file. 
    Using parser As New GenericParserAdapter("MyData.txt")
      parser.Load("MyData.xml")
      dsResult = parser.GetDataSet()
    End Using

    ' Or... programmatically setting up the parser for TSV. 
    Dim strID As String, strName As String, strStatus As String
    Using parser As New GenericParser()
      parser.SetDataSource("MyData.txt")

      parser.ColumnDelimiter = vbTab.ToCharArray()
      parser.FirstRowHasHeader = True
      parser.SkipStartingDataRows = 10
      parser.MaxBufferSize = 4096
      parser.MaxRows = 500
      parser.TextQualifier = """"c

      While parser.Read()
        strID = parser("ID")
        strName = parser("Name")

        ' Your code here ...
        strStatus = parser("Status")
      End While
    End Using

    ' Or... programmatically setting up the parser for Fixed-width. 
    Using parser As New GenericParser()
      parser.SetDataSource("MyData.txt")

      parser.ColumnWidths = New Integer(3) {10, 10, 10, 10}
      parser.SkipStartingDataRows = 10
      parser.MaxRows = 500

      While parser.Read()
        strID = parser("ID")
        strName = parser("Name")

        ' Your code here ...
        strStatus = parser("Status")
      End While
    End Using
  End Sub
End Class

There's also this from here, demonstrating DataTable usage:

Dim csv = "Name, Age" & vbCr & vbLf & "Ronnie, 30" & vbCr & vbLf & "Mark, 40" & vbCr & vbLf & "Ace, 50"

Dim reader As TextReader = New StringReader(csv)
Dim table = New DataTable()
Using it = reader.ReadCsvWithHeader().GetEnumerator()

  If Not it.MoveNext() Then
    Return
  End If

  For Each k As var In it.Current.Keys
    table.Columns.Add(k)
  Next

  Do
    Dim row = table.NewRow()
    For Each k As var In it.Current.Keys
      row(k) = it.Current(k)
    Next


    table.Rows.Add(row)
  Loop While it.MoveNext()
End Using

And this Q&A illustrates how to sort the DataTable by a given column.

InteXX
  • 6,135
  • 6
  • 43
  • 80