Sock
07-18-2013, 01:41 PM
Hi everyone, I hope this one is a little interesting. I've tried diving into sorting algorithms only to realize it's a pretty crazy place to go.
I have a single dimension array which can vary in size depending on the larger code's inputs. I want to sort this array from smallest to largest. Here's the catch: in this array there may be error values: CVErr(xlErrNA) to be exact. I'm unsure on how to incorporate these into the sorting situation.
Here's a contrived example with a five-item array. I've used the quicksort algorithm that I found from here: http://en.allexperts.com/q/Visual-Basic-1048/string-manipulation.htm (I hope linking this is okay; I just want to source where I have gotten things from)
Sub arraysorttest()
Dim arraytester(1 To 5) As Variant
arraytester(1) = 3
arraytester(2) = 2
arraytester(3) = CVErr(xlErrNA) 'Sort me! I dare you!
arraytester(4) = 1
arraytester(5) = 4
QuickSort arraytester, 1, UBound(arraytester)
End Sub
Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
Dim pivot As Variant
Dim tmpSwap As Variant
Dim tmpLow As Long
Dim tmpHi As Long
tmpLow = inLow
tmpHi = inHi
pivot = vArray((inLow + inHi) \ 2)
While (tmpLow <= tmpHi)
While (vArray(tmpLow) < pivot And tmpLow < inHi)
tmpLow = tmpLow + 1
Wend
While (pivot < vArray(tmpHi) And tmpHi > inLow)
tmpHi = tmpHi - 1
Wend
If (tmpLow <= tmpHi) Then
tmpSwap = vArray(tmpLow)
vArray(tmpLow) = vArray(tmpHi)
vArray(tmpHi) = tmpSwap
tmpLow = tmpLow + 1
tmpHi = tmpHi - 1
End If
Wend
If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi
End Sub
I'm really interested in how I might be able to sort through an array when there are some tricky error values in there.
Thank you so much for everything, everyone! You've all been of immeasurable help to me!
I have a single dimension array which can vary in size depending on the larger code's inputs. I want to sort this array from smallest to largest. Here's the catch: in this array there may be error values: CVErr(xlErrNA) to be exact. I'm unsure on how to incorporate these into the sorting situation.
Here's a contrived example with a five-item array. I've used the quicksort algorithm that I found from here: http://en.allexperts.com/q/Visual-Basic-1048/string-manipulation.htm (I hope linking this is okay; I just want to source where I have gotten things from)
Sub arraysorttest()
Dim arraytester(1 To 5) As Variant
arraytester(1) = 3
arraytester(2) = 2
arraytester(3) = CVErr(xlErrNA) 'Sort me! I dare you!
arraytester(4) = 1
arraytester(5) = 4
QuickSort arraytester, 1, UBound(arraytester)
End Sub
Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
Dim pivot As Variant
Dim tmpSwap As Variant
Dim tmpLow As Long
Dim tmpHi As Long
tmpLow = inLow
tmpHi = inHi
pivot = vArray((inLow + inHi) \ 2)
While (tmpLow <= tmpHi)
While (vArray(tmpLow) < pivot And tmpLow < inHi)
tmpLow = tmpLow + 1
Wend
While (pivot < vArray(tmpHi) And tmpHi > inLow)
tmpHi = tmpHi - 1
Wend
If (tmpLow <= tmpHi) Then
tmpSwap = vArray(tmpLow)
vArray(tmpLow) = vArray(tmpHi)
vArray(tmpHi) = tmpSwap
tmpLow = tmpLow + 1
tmpHi = tmpHi - 1
End If
Wend
If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi
End Sub
I'm really interested in how I might be able to sort through an array when there are some tricky error values in there.
Thank you so much for everything, everyone! You've all been of immeasurable help to me!