8 Replies Latest reply on Mar 24, 2014 12:48 PM by Todd Bennett

# Reorder numbers function

Does anyone have a function (or snippet) for reordering three variables highest to lowest?

I have three variables: dimA, dimB, and dimC (all as Double).

I need them reordered so that dimC is always the smallest number and dimA is always the largest.

dimA and dimB may be equal as long as they are larger than dimC.

dimB and dim C may be equal as long as they are smaller than dimA.

If dimA, dimB, and dimC are all equal, nothing needs to change.

I made some If, Then, Else statements but there is a bug I couldn't figure out (it has been deleted).

Thanks,

Todd

• ###### Re: Reorder numbers function

This works but it seems clunky.  I wouldn't want to use it with many more variables.

dim dblA as double

dim dblB as double

dim dblC as double

dim tempA as double

dim tempB as double

tempA = dblA

tempB = dblB

If dblC > dblB Then

dblB = dblC

dblC = tempB

End If

If dblB > dblA Then

dblA = dblB

dim = tempA

End If

If dblC > dblB Then

dblC = dblB

dblB = tempB

End If

• ###### Re: Reorder numbers function

Which language?

.Net (so, c#, VB or even VSTA macros) have access to System.Collections which all have Sort facilities

• ###### Re: Reorder numbers function

VBA

• ###### Re: Reorder numbers function

I looked up "bubble sort" and the improved "selection sort".  There are a few problems.

1) I haven't finished my coffee.

2) I don't know C++ (or how to convert it to VBA)

3) I'm not working with integers.

4) I haven't finished my coffee.

• ###### Re: Reorder numbers function

In VBA, you can use a Collection.

http://stackoverflow.com/questions/3587662/how-do-i-sort-a-collection

• ###### Re: Reorder numbers function

Here's my fix:

Function GnomeSort(ByRef pvarArray As Variant)

Dim i As Long

Dim j As Long

Dim iMin As Long

Dim iMax As Long

Dim varSwap As Variant

iMin = LBound(pvarArray) + 1

iMax = UBound(pvarArray)

i = iMin

j = i + 1

Do While i <= iMax

If pvarArray(i) < pvarArray(i - 1) Then

varSwap = pvarArray(i)

pvarArray(i) = pvarArray(i - 1)

pvarArray(i - 1) = varSwap

If i > iMin Then i = i - 1

Else

i = j

j = j + 1

End If

Loop

End Function

'---------------------------------------------------------------------------------

Private Sub XYZ()

Dim tempVar As Variant

Dim XYZvar(3) As Variant

Set swApp = CreateObject("SldWorks.Application")

Set Part = swApp.ActiveDoc

If bPartIsReady = False Or bAssembly = False Then

Corners = Part.GetPartBox(False)

XYZvar(1) = Abs(Corners(3) - Corners(0))

XYZvar(2) = Abs(Corners(5) - Corners(2))

XYZvar(3) = Abs(Corners(4) - Corners(1))

tempVar = GnomeSort(XYZvar)

txtX.Value = Format\$(XYZvar(3), "####.000") ' Length is always the longest side

txtY.Value = Format\$(XYZvar(2), "####.000") ' Width/height/depth is whatever is not length or thickness

txtZ.Value = Format\$(XYZvar(1), "####.000") ' Thickness is always the shortest side

End If

End Sub

• ###### Re: Reorder numbers function

Best way to do this is to assign your values to an array like so:

Option Explicit

Dim dblA, dblB, dblC, dblTemp As Double

Dim dblArray(0 To 2) As Double

Dim i, j As Integer

Sub main()

dblA = 3

dblB = 4

dblC = 1

dblArray(0) = dblA

dblArray(1) = dblB

dblArray(2) = dblC

For i = 0 To UBound(dblArray)

For j = i + 1 To UBound(dblArray)

If dblArray(i) < dblArray(j) Then

dblTemp = dblArray(i)

dblArray(i) = dblArray(j)

dblArray(j) = dblTemp

End If

Next j

Next i

dblA = dblArray(0)

dblB = dblArray(1)

dblC = dblArray(2)

End Sub

If you want to use more values just change the length of the array