排序字符串的意外返回()

时间:2014-07-20 19:56:21

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

  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)。 我不知道,希望有人可以帮助我。 谢谢。

1 个答案:

答案 0 :(得分:1)

will onyl获取你想要使用int,long,double的输出..但是使用字符串,它们按照lexycographically排序,这意味着..

1,113,23,3,5等......

所有1的首先..然后2' s等...;)

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

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

相关问题