Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用字符串项升序和降序对列表框进行排序_Vb.net_Sorting - Fatal编程技术网

Vb.net 使用字符串项升序和降序对列表框进行排序

Vb.net 使用字符串项升序和降序对列表框进行排序,vb.net,sorting,Vb.net,Sorting,如何对按钮进行编码,以便在点击列表框中的项目时按升序或降序排序(取决于单击的按钮)。它不能使用内置的Sort()来完成,而应该使用for循环来完成。试图对问题的解决方案进行伪编码,甚至“使用for循环将每个listbox项存储在一个数组中”。我将为您提供解决方案,但对于数字排序,您只需更改比较逻辑即可确定哪个字符串大于另一个字符串 步骤1:将项目从列表框复制到数组ar。 步骤2:使用快速排序: Private quickArr() As Integer Public Sub QuickSort

如何对按钮进行编码,以便在点击列表框中的项目时按升序或降序排序(取决于单击的按钮)。它不能使用内置的
Sort()
来完成,而应该使用
for
循环来完成。试图对问题的解决方案进行伪编码,甚至“使用for循环将每个listbox项存储在一个数组中”。

我将为您提供解决方案,但对于数字排序,您只需更改比较逻辑即可确定哪个字符串大于另一个字符串

步骤1:将项目从列表框复制到数组ar。 步骤2:使用快速排序:

Private quickArr() As Integer

Public Sub QuickSort(ByVal arr() As Integer)
    quickArr = arr
    DoQuickSort(0, arr.Length - 1)
End Sub

Private Sub DoQuickSort(ByVal low As Integer, ByVal high As Integer)
    Dim i As Integer = low
    Dim j As Integer = high
    Dim pivot As Integer = Math.Ceiling(quickArr(((low + high) / 2)))  'pivot is the middle element(ceiling)
    mMoves += 1
    While i <= j
        While quickArr(i) < pivot
            i += 1
        End While
        While quickArr(j) > pivot
            j -= 1
        End While
        If i <= j Then
            Dim t As Integer = quickArr(i)
            quickArr(i) = quickArr(j)
            quickArr(j) = t
            i += 1
            j -= 1
            mMoves += 2
        End If
    End While
    If low < j Then
        DoQuickSort(low, j)
    End If
    If i < high Then
        DoQuickSort(i, high)
    End If
End Sub
Private quickArr()作为整数
公共子快速排序(ByVal arr()为整数)
quickArr=arr
DoQuickSort(0,阵列长度-1)
端接头
私有子DoQuickSort(ByVal低为整数,ByVal高为整数)
Dim i作为整数=低
尺寸j为整数=高
将轴调整为整数=数学天花板(quickArr(((低+高)/2))'轴是中间元素(天花板)
mMoves+=1
当我旋转时
j-=1
结束时
如果我