升序文本框,以逗号分隔

时间:2018-12-29 02:06:44

标签: vb.net

我是新来的。我有一个文本框 Textbox1.text = 1,2,7,4,11。 我想输出: 1,2,4,7,11。

Textbox1.text = 1,2,7,4,11.
I want to be Output:
1,2,4,7,11.

VB.Net

我找到了这段代码,它适用于想要的人。

代码:

Private Sub Array()
    Dim InputNumbers, SplitInputNumbers, ArrayCount, ReSort, iterInputNum, Num1, Num2
    InputNumbers = OutputText1.Text
    SplitInputNumbers = Split(InputNumbers, ",")
    ArrayCount = UBound(SplitInputNumbers)
    ReSort = "YES"
    While ReSort = "YES"
        ReSort = "NO"
        For iterInputNum = 0 To ArrayCount
            If iterInputNum < ArrayCount Then
                If CInt(SplitInputNumbers(iterInputNum)) > CInt(SplitInputNumbers(iterInputNum + 1)) Then
                    Num1 = SplitInputNumbers(iterInputNum)
                    Num2 = SplitInputNumbers(iterInputNum + 1)
                    SplitInputNumbers(iterInputNum + 1) = Num1
                    SplitInputNumbers(iterInputNum) = Num2
                    ReSort = "YES"
                End If
            End If
        Next
    End While
    Dim iterSortedNum, SortedNumericArray
    For iterSortedNum = 0 To ArrayCount
        If iterSortedNum = 0 Then
            SortedNumericArray = SplitInputNumbers(iterSortedNum)
        Else
            SortedNumericArray = SortedNumericArray & "," & SplitInputNumbers(iterSortedNum)
        End If
    Next
    OutputText1.Text = (SortedNumericArray)

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作。它需要您的字符串将其拆分为一个数组。将每个子字符串转换为数字,生成一个新的Integer数组。对该新数组进行排序。然后使用join将其转换回逗号分隔的字符串

Dim str = "1,2,7,4,11"
Dim b = String.Join(",", str.Split(",").Select(Function(x) Integer.Parse(x.Trim())).OrderBy(Function(x) x))