根据列日期对数据进行排序

我有一个类似于以下的csv文件:

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

我的目标是根据上述csv的可用数据绘制折线图,​​实际的csv文件包含数千条折线。我的图表在x轴上具有Lower,Upper和Actual列,在y轴上具有日期(基于周数)。

我想根据日期(从最旧到最新)绘制图表。如果您注意到“日期”列,则这些值会被弄乱,并且不会基于日期升序排列。

我的计划是创建另一个csv文件(Sorted.csv),并将数据按升序放置,然后从此处进行图表绘制。我卡住了,因为找不到基于日期对所有数据进行排序的方法。如果能够创建Sorted.csv,则可以继续我的目标。这是我的示例函数:

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

任何帮助深表感谢。

吉米

有很多方法可以对各种类型的列表进行排序。
在这里我考虑的实现.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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章