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

问题描述 投票:2回答:2

我有一个与此类似的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

非常感谢任何帮助。

vb.net csv
2个回答
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

0
投票

因为您的日期存储在“MM / dd / yyyy HH:mm”格式中,所以您必须为排序方法提供一种比较方法,您可以在其中拆分日期值并重新排序它们的顺序,以便您获得更类似的内容: yyyy / MM / dd HH:mm“(最大元素优先)

总而言之:重新格式化你的约会并开心:)

© www.soinside.com 2019 - 2024. All rights reserved.