I'm struggling with finding a sorting algorithm that can sort the columns of a multidimensional array. All columns should be sorted individually (ascending) without a link with the other columns. I have tried to use the code proposed in this thread: Sorting a multidimensionnal array in VBA
But all I get is the value from the last entry in the last column.For instance if the first column is (3, 5, 1, 9) and the second column (5, 4, 8, 1) all I get are 1's in the whole array. What I want is (1, 3, 5, 9) in the first column and (1, 4, 5, 8) in the second.
I call the sort algorith with the following command: QuickSortArray Arraytosort, , , 1
to sort the first column. The plan was to later on make a loop to sort all columns.
I have two questions:
What am I doing wrong?
and can I use this code for this problem in the first place? Or does it need to be adjusted?
This is the code so far:
Sub P_Values()
Dim threshold As Long 'threshold
Dim N As Long 'number of samples
Dim M As Long 'number of sims
N = Cells(2, 2)
M = Cells(3, 2)
ReDim Samples(N, M) 'array to store samples
ReDim CM(M)
Dim teller_N As Long 'counter sample
Dim teller_M As Long 'counter sims
Dim teta As Double 'teta parameter
teta = Cells(3, 6)
threshold = Cells(1, 2)
'generate samples
For teller_M = 1 To M
For teller_N = 1 To N
Samples(N, M) = sev_expo(teta, threshold)
Cells(6 + teller_N, 1 + teller_M).Value = Samples(N, M)
Next
Next
'sort sims
QuickSortArray Samples(), , , 1
And the code from the function sev_expo:
Function sev_expo(ByVal teta As Double, ByVal threshold As Long) As Long
Dim U As Double
U = Rnd
sev_expo = -teta * Log(1 - U) + threshold
End Function