VB - 如何按降序对数字列表进行排序

时间:2016-12-08 22:06:42

标签: vb.net sorting

我正在使用Visual Basic为文本制作频率分析工具。下面的代码接收来自用户的输入,然后告诉他们每个字母在他们输入的文本中出现了多少,并且工作正常。但是,我正在努力按字母顺序排列。

    Dim words, output
    Dim count = 0
    Dim letters As Array = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
    words = UCase(textInputBox.Text)
    For Each x In letters
        For Each i In words
            If x = i Then
                count = count + 1
            End If
        Next
        If count <> 0 Then
            output = output & (x & " = " & count) & vbCrLf
            textOutputBox.Text = output
            letterReplaceBox.Text = words
        End If
        count = 0
    Next
End Sub

例如,输入文字: 你好 输出: E = 1

H = 1

L = 2

O = 1

无论如何我可以按顺序排列这些吗?我正在考虑用字母和数字尝试二维数组,然后从那里订购?或者将数字全部添加到列表中?或者使用类似冒泡的东西?或者有更有效的方法吗?我不确定如何接近它......

感谢您提出任何建议或帮助(:

2 个答案:

答案 0 :(得分:1)

可以使用LINQ。这将按字母对您的输入进行分组。这里有一些定制。首先是代码:

Sub Main()
    Dim input = "The quick brown fox jumps over the lazy dog"
    Dim result =
        input.
        ToUpper().
        GroupBy(Function(c) c).
        OrderByDescending(Function(g) g.Count).
        Where(Function(g) Asc(g.First) >= 65 And Asc(g.First) <= 90).
        Select(Function(g) String.Format("'{0}' = {1}", g.First, g.Count))
    For Each r In result
        Console.WriteLine(r)
    Next
End Sub

这部分代码将输入转换为大写(当然你可以将其保留,但请参阅下面的条件来过滤字母)。

ToUpper()

这是按字母分组的

GroupBy(Function(c) c)

这按元素数量

命令
OrderByDescending(Function(g) g.Count)

除了大写字母(没有空格,标点符号等,请参阅http://www.asciitable.com/

Where(Function(g) Asc(g.First) >= 65 And Asc(g.First) <= 90)

这将返回带有字母和出现次数的格式化字符串。

Select(Function(g) String.Format("'{0}' = {1}", g.First, g.Count))

当然,你可以返回任何你想要的东西,但是我为演示做了这个。

这是代码的输出:

  

&#39; O&#39; = 4   
&#39; E&#39; = 3   
&#39; T&#39; = 2   
&#39; H&#39; = 2   
&#39; U&#39; = 2   
&#39; R&#39; = 2   
&#39; Q&#39; = 1   
&#39;我&#39; = 1   
&#39; C&#39; = 1   
&#39; K&#39; = 1   
&#39; B&#39; = 1   
&#39; W&#39; = 1   
&#39; N&#39; = 1   
&#39; F&#39; = 1   
&#39; X&#39; = 1   
&#39; J&#39; = 1   
&#39; M&#39; = 1   
&#39; P&#39; = 1   
&#39; S&#39; = 1   
&#39; V&#39; = 1   
&#39; L&#39; = 1   
&#39; A&#39; = 1   
&#39; Z&#39; = 1   
&#39; Y&#39; = 1   
&#39; d&#39; = 1   
&#39; G&#39; = 1

答案 1 :(得分:1)

另一个例子:

    Dim frequencies As New Dictionary(Of String, Tuple(Of String, Integer))
    For Each letter As String In textInputBox.Text.ToUpper.ToArray
        If Not frequencies.ContainsKey(letter) Then
            frequencies.Add(letter, New Tuple(Of String, Integer)(letter, 1))
        Else
            frequencies(letter) = New Tuple(Of String, Integer)(letter, frequencies(letter).Item2 + 1)
        End If
    Next

    Dim results = frequencies.Values.OrderByDescending(Function(x) x.Item2).ToList
    Dim output As New System.Text.StringBuilder
    For Each pair In results
        output.AppendLine(pair.Item1 & " = " & pair.Item2)
    Next
    textOutputBox.Text = output.ToString