如何按日期冒泡排序对细节进行分组和排序?

时间:2013-09-01 01:59:03

标签: vb.net

首先感谢您阅读本文,我花了最后四个小时试图解决这个问题。

基本上我正在构建一个用户输入的应用程序:日期,名称,电话号码和教师名称到一个简单的csv .txt数据库文件。我已经完成了所有工作。

现在我需要做的就是以某种方式将细节组合在一起,并与其他条目分开。

我现在想要通过冒泡排序按日期对这些分组的详细信息进行排序,然后将其保存到另一个文件中。当我说排序时,我希望其他细节与日期一致。

输入申请的日期必须是:(yyMMddhhmm) 例如:1308290930 = 9:30 on 29/08/13

我可以发布到目前为止我所做的事情。

Public Class Form2
    Dim currentRow As String()
    Dim count As Integer
    Dim one As Integer
    Dim two As Integer
    Dim three As Integer
    Dim four As Integer

    Dim catchit(100) As String
    Dim count2 As Integer
    Dim arrayone(50) As Integer
    Dim arraytwo(50) As String
    Dim arraythree(50) As Integer
    Dim arrayfour(50) As String

    Dim bigstring As String
    Dim builder As Integer
    Dim twodata As Integer

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Me.RichTextBox1.LoadFile("D:\completerecord.txt", RichTextBoxStreamType.PlainText)

        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("D:\completerecord.txt")
            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")
            Dim currentRow As String()
            Dim count As Integer
            count = 0
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()
                    Dim currentField As String
                    For Each currentField In currentRow
                        ' makes one array to contain a record for each peice of text in the file
                        'MsgBox(currentField) '- test of Field Data
                        ' builds a big string with new line-breaks for each line in the file

                        bigstring = bigstring & currentField + Environment.NewLine

                        'build two arrays for the two columns of data
                        If (count Mod 2 = 1) Then
                            arraytwo(two) = currentField
                            two = two + 1
                            'MsgBox(currentField)
                        ElseIf (count Mod 2 = 0) Then
                            arrayone(one) = currentField
                            one = one + 1
                        End If

                        count = count + 1
                        'MsgBox(count)
                    Next

                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Error Occured, Please contact Admin.")
                End Try
            End While
        End Using
        RichTextBox1.Text = bigstring
        ' MsgBox("test")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NoMoreSwaps As Boolean
        Dim counter As Integer

        Dim Temp As Integer
        Dim Temp2 As String
        Dim listcount As Integer
        Dim builder As Integer
        Dim bigString2 As String = ""

        listcount = UBound(arraytwo)
        'MsgBox(listcount)
        builder = 0
        'bigString2 = ""
        counter = 0
        Try
            'this should sort the arrays using a Bubble Sort
            Do Until NoMoreSwaps = True
                NoMoreSwaps = True
                For counter = 0 To (listcount - 1)
                    If arraytwo(counter) > arraytwo(counter + 1) Then
                        NoMoreSwaps = False

                        If arraytwo(counter + 1) > 0 Then

                            Temp = arraytwo(counter)
                            Temp2 = arrayone(counter)

                            arraytwo(counter) = arraytwo(counter + 1)
                            arrayone(counter) = arrayone(counter + 1)

                            arraytwo(counter + 1) = Temp
                            arrayone(counter + 1) = Temp2
                        End If
                    End If
                Next
                If listcount > -1 Then
                    listcount = listcount - 1
                End If
            Loop

            'now we need to output arrays to the richtextbox first we will build a new string
            'and we can save it to a new sorted file
            Dim FILE_NAME As String = "D:\sorted.txt"

            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)

                While builder < listcount
                    bigString2 = bigString2 & arraytwo(builder) & "," & arrayone(builder) + Environment.NewLine

                    objWriter.Write(arraytwo(builder) & "," & arrayone(builder) + Environment.NewLine)
                    builder = builder + 1
                End While
                RichTextBox2.Text = bigString2

                objWriter.Close()
                MsgBox("Text written to log file")
            Else
                MsgBox("File Does Not Exist")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

创建一个类来保存每个条目的信息,如下所示:

Public Class MyEntry
    Public Property TheDate() As DateTime
        Get
            Return m_Date
        End Get
        Set
            m_Date = Value
        End Set
    End Property
    Private m_Date As DateTime
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property PhoneNumber() As String
        Get
            Return m_PhoneNumber
        End Get
        Set
            m_PhoneNumber = Value 
        End Set
    End Property
    Private m_PhoneNumber As String
    Public Property Instructor() As String
        Get
            Return m_Instructor
        End Get
        Set
            m_Instructor = Value
        End Set
    End Property
    Private m_Instructor As String

    Public Sub New(date As DateTime, name As String, phoneNumber As String, instructor As String)
        TheDate = date
        Name = name
        PhoneNumber = phoneNumber
        Instructor = instructor
    End Sub
End Class

现在您可以创建上述类的列表,如下所示:

Private entries As var = New List(Of MyEntry) From { _
    New MyEntry(DateTime.Now.AddDays(-1), "Dummy 1", "555-123-4567", "Instructor A"), _
    New MyEntry(DateTime.Now.AddDays(-1), "Dummy 2", "555-124-4567", "Instructor B"), _
    New MyEntry(DateTime.Now.AddDays(-1), "Dummy 3", "555-125-4567", "Instructor C"), _
    New MyEntry(DateTime.Now.AddDays(-2), "Dummy 4", "555-126-4567", "Instructor A"), _
    New MyEntry(DateTime.Now.AddDays(-2), "Dummy 5", "555-127-4567", "Instructor B") _
}

注意:您需要在此处替换您的实际值,并使用某种类型的循环结构来执行此操作。

现在,您可以将LINQ GroupBy函数应用于条目列表,如下所示:

Private entriesByDate As var = entries.GroupBy(Function(x) x.TheDate).ToList()

这会产生上面创建的虚拟数据的两个条目列表,您的分组数量将根据您的实际数据而变化。

现在你可以遍历组列表,如下所示:

For Each entry In entriesByDate
    ' Put logic here to save each group to file
Next

答案 1 :(得分:0)

我的建议是在达到重新校正的末尾添加一个标记(日期,时间等)。我用“|”。然后,当您读回数据时,将记录拆分为一个数组,并使用它读出它们。

所以它会是: 130829|0930|<name>|<phone number>|etc

你明白吗?