将数组按字母顺序排列vb

时间:2015-06-04 19:24:09

标签: arrays vb.net

我的文件中充满了团队名称和分数,如下所示:

Team A
43
65
39
Team B
72
68
62
...

我认为最好的方法是把它们拿出来放在一个数组中(其他建议会很有帮助)。我希望按字母顺序显示团队的最高分。我想我会修剪除了第一个字母或名称之外的所有内容,并使用char函数转换为数字然后进行比较。这将如何运作或有更好的想法?我怎样才能找到最高分?

谢谢和回复将不胜感激。我是视觉基础和编程的新手,即使效率稍差,也是最好的。

2 个答案:

答案 0 :(得分:0)

使用List

'You can also store Integers, Doubles, whatever you like
Dim lstNames As New List(Of String)

lstNames.Add("Bean")
lstNames.Add("Apple")
lstNames.Add("Corn")

lstNames.Sort()

'Apple
'Bean
'Corn
Dim lstScores As New List(Of Integer)

lstScores.Add(10)
lstScores.Add(100)
lstScores.Add(1)

lstScores.Sort()

'1
'10
'100

List内置了一个排序功能。它默认按字母顺序排列,但您可以创建自己的。

如果存储了整数,则最高值将在最后。

Dim nHighestScore As Integer = lstScores.Last

答案 1 :(得分:0)

我为你准备了一些代码 - 阅读评论以获取更多信息 - 虽然我不确定你的文件是如何格式化的,因为有人编辑了你的帖子。这两种解决方案如下。

    Dim filetext As String = My.Computer.FileSystem.ReadAllText(filename) 'read your file
    Dim teamlist As List(Of String) = New List(Of String) 'Just a variable, using it later

    Dim YourFileIsSeperatedByNewline As Boolean = False

    If YourFileIsSeperatedByNewline Then
        Dim splitfile As String() = Split(filetext, vbCrLf) 'Split your file, assuming that it is seperated by newline
        If splitfile.Count = 0 Then : End If 'insert some exit because there are no items
        Dim current As Integer = 0 'So you know which you are at
        Dim scorelist(3) As Integer 'no of scores must be exactly 3
        Dim team As String = ""
        For i = 0 To splitfile.Count - 1
            If current = 0 Then
                team &= splitfile(i)
            ElseIf current = 1 Or current = 2 Then
                scorelist(current - 1) = splitfile(i)
            Else
                scorelist(current - 1) = splitfile(i) 'One more
                teamlist.Add(team & " " & Math.Max(Math.Max(scorelist(0), scorelist(1)), scorelist(2)))
                current = -1 'make current=0 the next loop
                team = "" 'Reset
            End If
            current += 1
        Next
    Else
        'If your file is seperated by spaces (Team A ... ... ...) and so on
        Dim splitfile = Split(filetext, " ")
        If splitfile.Count = 0 Then : End If 'insert some exit because there are no items
        Dim current As Integer = 0 'So you know which you are at
        Dim scorelist(3) As Integer 'no of scores must be exactly 3
        Dim team As String = ""
        For i = 0 To splitfile.Count - 1
            If current = 0 Or current = 1 Then
                team &= splitfile(i)
            ElseIf current = 2 Or current = 3 Then
                scorelist(current - 2) = splitfile(i)
            Else
                scorelist(current - 2) = splitfile(i) 'One more
                teamlist.Add(team & " " & Math.Max(Math.Max(scorelist(0), scorelist(1)), scorelist(2)))
                current = -1 'make current=0 the next loop
                team = "" 'Reset
            End If
            current += 1
        Next
    End If


    teamlist.Sort()
    MsgBox(Join(teamlist.ToArray, vbCrLf)) 'then display teamlist (can be changed)