在Visual Basic中对数据库结构进行排序

时间:2016-11-01 21:07:29

标签: vb.net sorting structure

我有这个结构:

Module Global_Variables
    Public myrecords(10) As myDatabase
    Public counter As Integer = 0

    Public Structure myDatabase
        'creates database Structure For input data
        <VBFixedString(30)> _
        Dim Driver As String           '30 bytes
        Dim Car As Integer             '4 bytes
        <VBFixedString(15)> _
        Dim Team As String             '15 bytes
        Dim Grid As Integer            '4 bytes
        Dim FastestLap As Double       '8 bytes
        Dim RaceTime As Double         '4 bytes
        Dim Points As Double           '4 bytes

    End Structure
End Module

程序从用户接收数据,然后以另一种形式在名为txtOutput的文本框中显示数据:

myrecords(counter).Driver = driver_box.Text
myrecords(counter).Car = car_box.Text
myrecords(counter).Team = team_box.Text
myrecords(counter).Grid = grid_box.Text
myrecords(counter).FastestLap = fl_box.Text
myrecords(counter).RaceTime = rt_box.Text
myrecords(counter).Points = points_box.Text

Form_Display.txtDisplay.AppendText(myrecords(counter).Driver & "          " & 
    myrecords(counter).Car & "          " & myrecords(counter).Team & "          " & 
    myrecords(counter).Grid & "             " & myrecords(counter).FastestLap & "             " & 
    myrecords(counter).RaceTime & "          " & myrecords(counter).Points & vbCrLf)

counter = counter + 1
MsgBox("Submit success!")
Call input_box_clear()

然后,用户可以单击按钮以最快圈速按升序对记录进行排序。我该怎么做?

我尝试过像冒泡排序和选择排序等算法,但都没有效果。

谢谢

1 个答案:

答案 0 :(得分:1)

声明类型(类,不是结构)

Public class RaceData
    Public Property Driver As String
    Public Property Car As Integer
    Public Property Team As String
    Public Property Grid As Integer
    Public Property FastestLap As Double
    Public Property RaceTime As Double
    Public Property Points As Double
End Class

内存数据库(查看System.Collections中的内容)

Private _raceDb As New List(Of RaceData)()

添加用户输入

Dim newItem As New RaceData()
newItem.Driver = driver_box.Text
newItem.Car = Integer.Parse(car_box.Text)
newItem.Team = team_box.Text
newItem.Grid = Integer.Parse(grid_box.Text)
newItem.FastestLap = Double.Parse(fl_box.Text)
newItem.RaceTime = Double.Parse(rt_box.Text)
newItem.Points = Double.Parse(points_box.Text)
_raceDb.Add(newItem)

为网格排序(阅读LINQ)

// sort by fastest race time
Dim sortedDb As List(Of RaceData) = _raceDb.OrderBy(Function(x) x.RaceTime).ToList()

选择一场最快的比赛

Dim fastest As RaceData = _raceDb.OrderBy(Function(x) x.RaceTime).FirstOrDefault()
If fastest IsNot Nothing Then ...

为每个项目构建一个字符串以添加到多行文本框

Dim lines() As String = _raceDb.Select(Function(x) x.Driver & " --- " & x.Team).ToArray()
' Using some tips from the comments
Dim lines() As String = _raceDb.
    Select(Function(x) string.Format("{0,-30} --- {1,15}", x.Driver, x.Team)).ToArray()