Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default sorting an array

Hello,

I use a sorting function (BubbleSort) to sort an array of data. The array
is 3 dimentions and it is being sorted by the first. I want it to sort by
the first then the second. Can anyone help me with this?

Here is the code now:

Function BubbleSort(TempArray As Variant)

Dim temp(71, 3) As Variant
Dim i As Integer
Dim NoExchanges As Integer

' Loop until no more "exchanges" are made.

Do

NoExchanges = True

' Loop through each element in the array.

For i = 1 To UBound(TempArray) - 1

' If the element is greater than the element
' following it, exchange the two elements.

If TempArray(i, 1) TempArray(i + 1, 1) Then
NoExchanges = False

temp(i, 1) = TempArray(i, 1)
temp(i, 2) = TempArray(i, 2)
temp(i, 3) = TempArray(i, 3)

TempArray(i, 1) = TempArray(i + 1, 1)
TempArray(i, 2) = TempArray(i + 1, 2)
TempArray(i, 3) = TempArray(i + 1, 3)

TempArray(i + 1, 1) = temp(i, 1)
TempArray(i + 1, 2) = temp(i, 2)
TempArray(i + 1, 3) = temp(i, 3)

End If

Next i

Loop While Not (NoExchanges)

End Function

Thanks,
Ernst.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default sorting an array

Hi Ernst,
firstly, its a two dimensional array, not three! Secondly, I **could**
post some complicated code I've found on the web, but would it not be
easier to copy the array to a worksheet and use Excel's Sort routine?

OJ

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default sorting an array

post some complicated code I've found on the web, but would it not be
easier to copy the array to a worksheet and use Excel's Sort routine?


I need to step through the array after it is sorted before I put the data
back on a worksheet. So the excel sort routine is of no use to me here...

Ernst.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default sorting an array

Here is an example of how you might do it:

Sub BldArray()
Dim v As Variant
ReDim v(1 To 73, 1 To 3)
For i = 1 To 73
v(i, 1) = Int(Rnd * 5 + 1)
v(i, 2) = Int(Rnd * 10 + 1)
v(i, 3) = Int(Rnd * 100 + 1)
Next
Range("A1:C73").Value = v
BubbleSort v, 1, 73, 1
m = 1
l = v(m, 1)
i = m
Do
If v(m, 1) < l Then
Debug.Print i, m - 1, v(i, 1)
BubbleSort v, i, m - 1, 2
i = m
l = v(i, 1)
End If
m = m + 1
Loop While m <= UBound(v)
BubbleSort v, i, UBound(v), 2
Range("E1:G73").Value = v
End Sub









Function BubbleSort(TempArray, ii, jj, kk)

Dim temp(3) As Variant
Dim i As Integer
Dim NoExchanges As Integer

' Loop until no more "exchanges" are made.

Do

NoExchanges = True

' Loop through each element in the array.

For i = ii To jj - 1

' If the element is greater than the element
' following it, exchange the two elements.

If TempArray(i, kk) TempArray(i + 1, kk) Then
NoExchanges = False

temp(1) = TempArray(i, 1)
temp(2) = TempArray(i, 2)
temp(3) = TempArray(i, 3)

TempArray(i, 1) = TempArray(i + 1, 1)
TempArray(i, 2) = TempArray(i + 1, 2)
TempArray(i, 3) = TempArray(i + 1, 3)

TempArray(i + 1, 1) = temp(1)
TempArray(i + 1, 2) = temp(2)
TempArray(i + 1, 3) = temp(3)

End If

Next i

Loop While Not (NoExchanges)

End Function

--
Regards,
Tom Ogilvy

"Ernst Guckel" wrote in message
...
post some complicated code I've found on the web, but would it not be
easier to copy the array to a worksheet and use Excel's Sort routine?


I need to step through the array after it is sorted before I put the data
back on a worksheet. So the excel sort routine is of no use to me here...

Ernst.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default sorting an array

I think I understand what you are trying to do with this but I cannot seem to
implement it into my situation...

I load my array as follows:

'The Array's
Dim TheArray(71, 3) As Variant
Dim MgrArray(71, 3) As Variant

For Each C In Range(objWeek)

a = a + 1

If C.Cells(1, intLocation - 1).Value = "MGR" Or _
C.Cells(1, intLocation - 1).Value = "AM3" Or _
C.Cells(1, intLocation - 1).Value = "SS" Then

If C.Cells(1, intLocation - 1).Value = "MGR" Then

MgrArray(a, 1) = C.Value
MgrArray(a, 2) = C.Cells(1, 2).Value
MgrArray(a, 3) = C.Cells(1, intLocation).Value

End If

GoTo Skip

End If

If Application.WorksheetFunction.IsNumber(C.Value) = True Then
intStatus = intStatus + 1
End If

TheArray(a, 1) = C.Value
TheArray(a, 2) = C.Cells(1, 2).Value
TheArray(a, 3) = C.Cells(1, intLocation).Value

Skip:

Next

' Sort the Arrays
BubbleSort TheArray
BubbleSort MgrArray

....


Thanks again,
Ernst.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default sorting an array


Tom,

Thanks for the help. I figured it out.

BTW: My code for plotting the array on a worksheet is really slow... Any
ideas?

'Plot the values

R1 = 22
R2 = 22
Salary = 22

For a = 1 To UBound(TheArray)

varStart = TheArray(a, 1)
varStop = TheArray(a, 2)
strName = TheArray(a, 3)

If varStart = "" Then GoTo Bottom
If Application.WorksheetFunction.IsText(varStart) = True Then GoTo Bottom

'Determine which side and spacing

intCount = intCount + 1

If Not Lunch Then
If varStart = 0.458 Then
If bolPlotRight Then
R2 = R2 + 2
End If
R1 = R1 + 2
Lunch = True
End If
End If

If Not After Then
If varStart = 0.58 Then
If bolPlotRight Then
R2 = R2 + 2
End If
Salary = R1
R1 = R1 + 12
After = True
End If
End If

If Not Dinner Then
If varStart = 0.708 Then
If bolPlotRight Then
R2 = R2 + 2
End If
R1 = R1 + 2
Dinner = True
End If
End If

If Not Late Then
If varStart = 0.833 Then
If bolPlotRight Then
R2 = R2 + 2
End If
R1 = R1 + 2
Late = True
End If
End If

DayShift:

If R1 = 74 Then
bolPlotRight = True
GoTo PlotRight
End If

PlotLeft:

'Place names and Times
Range("B" & R1).Value = varStart
Range("C" & R1).Value = varStop
Range("D" & R1).Value = strName

R1 = R1 + 2

PlotRight:

If Not bolPlotRight Then GoTo Bottom

'Place names and Times
Range("BE" & R2).Value = varStart
Range("BF" & R2).Value = varStop
Range("BG" & R2).Value = strName

R2 = R2 + 2

Bottom:
Application.StatusBar = "Please wait ... " & Format(intCount / intStatus,
"percent") & " Complete."

Next

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting within an array Steve Excel Discussion (Misc queries) 1 May 31st 07 12:49 PM
Array Sorting in VB Randall[_4_] Excel Programming 1 August 24th 04 10:35 PM
Sorting 2D portion of 5D Array ExcelMonkey[_68_] Excel Programming 2 February 4th 04 04:04 AM
Sorting 2D Array ExcelMonkey[_28_] Excel Programming 14 January 28th 04 07:32 PM
Sorting a 2-dimensional array Vasant Nanavati[_2_] Excel Programming 3 July 12th 03 08:27 PM


All times are GMT +1. The time now is 11:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"