Function ArraySort(aSort, intAsc) Dim intTempStore Dim i, j For i = 0 To UBound(aSort) - 1 For j = i To UBound(aSort) 'Sort Ascending If intAsc = 1 Then If aSort(i) > aSort(j) Then intTempStore = aSort(i) aSort(i) = aSort(j) aSort(j) = intTempStore End if 'i > j 'Sort Descending Else if aSort(i) < aSort(j) Then intTempStore = aSort(i) aSort(i) = aSort(j) aSort(j) = intTempStore End if 'i < j End if 'intAsc = 1 Next 'j Next 'i ArraySort = aSort End Function
Function ArraySort(aSort, intAsc) Dim intTempStore Dim i, j For i = 0 To UBound(aSort) - 1 For j = i To UBound(aSort) 'Sort Ascending If intAsc = 1 Then If aSort(i) > aSort(j) Then intTempStore = aSort(i) aSort(i) = aSort(j) aSort(j) = intTempStore End if 'i > j 'Sort Descending Else if aSort(i) < aSort(j) Then intTempStore = aSort(i) aSort(i) = aSort(j) aSort(j) = intTempStore End if 'i < j End if 'intAsc = 1 Next 'j Next 'i ArraySort = aSort End Function
Du skal putte resultatet af sorteringen over i et nyt array:
SorteretArray = ArraySort(MyArray(0),1)
Dette VB Script illustrerer brugen:
Function ArraySort(aSort, intAsc) Dim intTempStore Dim i, j For i = 0 To UBound(aSort) - 1 For j = i To UBound(aSort) 'Sort Ascending If intAsc = 1 Then If aSort(i) > aSort(j) Then intTempStore = aSort(i) aSort(i) = aSort(j) aSort(j) = intTempStore End if 'i > j 'Sort Descending Else if aSort(i) < aSort(j) Then intTempStore = aSort(i) aSort(i) = aSort(j) aSort(j) = intTempStore End if 'i < j End if 'intAsc = 1 Next 'j Next 'i ArraySort = aSort End Function
Dim aRaw(10)
For i = 0 to 10 aRaw(i) = Round(Rnd*100) Next
' Sortér stigende aSorted = ArraySort(aRaw, 1)
strMsg = "Stigende:" & vbCRLF For i = 0 to UBound(aSorted) strMsg = strMsg & aSorted(i) & vbCRLF Next MsgBox strMsg
' Sortér faldende aSorted = ArraySort(aRaw, 0) strMsg = "Faldende:" & vbCRLF For i = 0 to UBound(aSorted) strMsg = strMsg & aSorted(i) & vbCRLF Next MsgBox strMsg
Super... Den havde jeg lige overset.. Tak for hjælpen :o)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.