I have a csv file similar to this:
Test Case Lower Upper Actual Date
Measure 2 8 3 4/14/2016 9:18
Measure 2 8 3 4/14/2016 11:16
Measure 2 8 5 4/12/2016 19:19
Measure 2 8 7 4/22/2016 10:36
Measure 2 8 6 4/22/2016 12:39
My goal is to plot a line chart from the data available from the above csv, the actual csv file contains a few thousands of line. My chart will have the Lower,Upper and Actual column in the x-axis and the date in the y-axis(Based on the week number).
I wanted to plot the chart based on the date (From oldest to latest). If you notice the "Date" column, the values are messed up and not in ascending order based on the date.
My plan is to create another csv file (Sorted.csv) and place the data in ascending order and perform my chart plotting from there. I stuck as I couldn't find a way to sort all the data based on the date. If I am able to get the Sorted.csv created, I can continue with my goal. This is my sample function:
Private Function sortCSV()
For Each rawRows As String In File.ReadLines("C:\sampleVBPrograms\SimplePlot\SomeFile.csv")
'I wanted to sort each line and place into a new csv "Sorted.csv" here
'Just stuck in the way to sort, writing into Sorted.csv, I can do
Next
End Function
Any help is deeply appreciated.
Hari
答案 0 :(得分:1)
有多种方法可以对各种类型的列表进行排序
在这里,我考虑.OrderBy()
和.Sort()
的实现。
用作从源文件读取的数据的容器的类。
Public Class Lines
Public Property TestCase As String
Public Property Lower As Single
Public Property Upper As Single
Public Property Actual As Single
Public Property ItemDate As DateTime
End Class
创建一个List(Of Lines)并填入您的数据:
编辑:将分隔符设置为逗号(chr(44))。
Dim MyLines As New List(Of Lines)()
Dim _FirstLine() As String
Dim _line() As String
Using _reader As TextReader = New StreamReader("C:\sampleVBPrograms\SimplePlot\SomeFile.csv")
'FirstLine keeps the Header of the following data
_FirstLine = _reader.ReadLine().Split(Chr(44))
'Read the file and split using Comma as delimiter
While (_reader.Peek() >= 0)
_line = _reader.ReadLine().Split(Chr(44))
'The Date Field is parsed using the InvariatCulture Comparer
'See if this setting suits your needs
MyLines.Add(New Lines With {.TestCase = _line(0),
.Lower = CType(_line(1), Single),
.Upper = CType(_line(2), Single),
.Actual = CType(_line(3), Single),
.ItemDate = Date.Parse(_line(4), CultureInfo.InvariantCulture)})
End While
End Using
'Order the list using a verbose .OrderBy()
Dim OrderedMyLines As IOrderedEnumerable(Of Lines) =
MyLines.OrderBy(Function(t As Lines) t.ItemDate, Comparer(Of DateTime).Default)
另一种使用Sort()的方法
我为此使用了自定义比较器。由于比较日期,您可能需要根据您的文化风格进行调整。
Public Class LinesComparer
Implements IComparer(Of Lines)
'Implements a Compare method for IComparer.
'See that the date evaluation differs from the one used in .OrderBy()
Public Function Compare(x As Lines, y As Lines) As Integer Implements IComparer(Of Lines).Compare
Return x.ItemDate.CompareTo(y.ItemDate)
End Function
End Class
'Sort the list using the custom Comparer
Dim LinesComp As LinesComparer = New LinesComparer()
MyLines.Sort(0, MyLines.Count, LinesComp)
修改强>
将任何有序列表写入文件:
Using _writer As TextWriter = New StreamWriter("C:\sampleVBPrograms\SimplePlot\sorted.csv")
_writer.WriteLine(String.Join(Chr(44), _FirstLine, 0, _FirstLine.Length)
For Each Line As Lines In OrderedMyLines
_writer.WriteLine(Line.TestCase + Chr(44) +
Line.Lower.ToString + Chr(44) +
Line.Upper.ToString + Chr(44) +
Line.Actual.ToString + Chr(44) +
Line.ItemDate.ToString)
Next
End Using
答案 1 :(得分:0)
因为您的日期存储在" MM / dd / yyyy HH:mm"格式化你必须给排序方法一个比较方法,你可以在这里分割你的日期值并重新排序它们的顺序,这样你就可以获得更像的东西:" yyyy / MM / dd HH:mm" (最重要的元素)
总而言之:重新格式化你的约会并感到高兴:)