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

时间:2010-11-24 18:38:20

标签: vb.net sorting

如何对一个按钮进行编码,一旦按下它,列表框中的项目将按升序或降序排序(取决于单击的按钮)。不能使用内置Sort(),而是使用for循环。尝试伪代码解决问题的方法,直到“将每个列表框项目存储在带有for循环的数组中”。

1 个答案:

答案 0 :(得分:0)

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

步骤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

将resuly数组3复制回列表框

相关问题