【问题标题】:Unexpected return of sorting a string()排序字符串()的意外返回
【发布时间】:2014-07-20 19:56:20
【问题描述】:
  Private Sub bttSort_Click(sender As Object, e As EventArgs) Handles bttSort.Click
        DataView1.Rows.Clear()
        Dim i As Integer
        Dim j As Integer
        Dim temp As Integer
        For i = 0 To list1.Length - 1
            For j = (i + 1) To list1.Length - 1
                If list1(i) > list1(j) Then
                    temp = list1(i)
                    list1(i) = list1(j)
                    list1(j) = temp
                End If
            Next
        Next
        DataView1.Rows.Clear()
        For m As Integer = 0 To list1.Length - 1
            DataView1.Rows.Add(list1(m))
        Next
    End Sub

我输入了一个字符串(1,3,5,6,7,113,23,62) 运行上面的代码后,输出应该排序为(1, 3, 5, 6, 7, 23, 62, 113 )。但我的输出是 (1, 113, 23, 3, 5, 6, 62, 7)。 我不知道,希望有人可以帮助我。 谢谢。

【问题讨论】:

    标签: arrays vb.net string loops visual-studio-2012


    【解决方案1】:

    onyl 会得到你想要的输出,使用 int、long、double.. 但使用字符串时,它们会按字典顺序排序,这意味着..

    1、113、23、3、5等......

    所有 1 的第一个..然后 2 的...等等... ;)

    将所有元素转换为整数,然后再试一次! ;)

    您将获得预期的输出! ;)

    【讨论】:

    • 谢谢你的回答,但你能告诉我怎么做吗?我试过“CInt”、“Array.ConvertAll”...但没有任何效果。
    • 我认为,如果您创建一个 int 数组,然后例如在字符串数组中使用 for 循环,则使用 CInt 将所有值复制到新的 int 数组...它将解决您的问题问题;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2015-11-10
    • 2012-12-05
    • 2015-09-12
    相关资源
    最近更新 更多