Sorting A 2 Dimensional Array

ceaser

Active Member
Licensed User
:BangHead:Hi

I wonder if somebody can help me:sign0085:

I have a 2 dimensional array : Dim Type(Offset,Level) OGL(0) as double which is declared under my Global Sub.

Now because of different calculations, i.e Toe Points, Shouder Break Points, Surface, Shoulders, Kerbs, etc., the Offsets and levels are all mixed up, i.e they do not run from small offsets to large offsets.

i.e.

(-20,1234); (10,1211); (-10,1212); (5,1213); (18,1211.5); (-2,1213)...etc.

Now what I want to do is to sort the Offsets from small to large, but the levels must stay with the Offsets...i.e.

(-20,1234); (-10,1212); (-2;1213); (5,1213); (18,1211.5);...etc.:BangHead:

Can this be done or is there maybe a function in B4PPC that I do not know of. I know of the "Sort" function, but that is for a 1 dimensional array.:BangHead:

Thanks
Michael
 

mjcoon

Well-Known Member
Licensed User
I appreciate that this is rather a provocative reply, but if the data had been in a table then the rows could be sorted by any column...

HTH (!) Mike.
 

specci48

Well-Known Member
Licensed User
Longtime User
Maybe it's possible to split the 2-dimensional array into two 1-dimensional.
The first array contains the offset, the second contains the level.

With this and the help of agrahams ArrayEx.dll you can use the method SortKeysAndItems sorting both arrays as requested.


specci48
 

ceaser

Active Member
Licensed User
Gentlemen

I am most obliged for your help:sign0188:

My problem has been solved.

Regards
Michael
 
Last edited:

ceaser

Active Member
Licensed User
Hi mjcoon

It's a short little routine:

'Sort The Offsets i1=number of Offsets & Levels
For i4=1 To i1-1
For i5=i4+1 To i1
If Offset(i4)>Offset(i5) Then
TOffset=Offset(i5) :TLevel=Design(i5)
Design(i5)=Design(i4) :Offset(i5)=Offset(i4)
Design(i4)=TLevel :Offset(i4)=TOffset
End If
Next i5
Next i4
 
Last edited:

DarkMann

Member
Licensed User
Longtime User
Faster Sort

Hi Ceaser

If your list of points is more than about 100, then your sort will be incredibly slow. As I have never gotten on well with a true recursive quicksort and never tried in B4P, I use the O-Sort written by Erik Oosterwal. Information used to be on geocities, but it has been taken down and almost forgotten.

It is an order of magnitude or more quicker than your bubble sort. The following code should drop into your program, but I have not tested it. It has been pulled from another program and changed to match your variable names.

B4X:
'o-sort for i1 entries
SngPhi = 0.78
SngFib = i1 * SngPhi
s = Int(SngFib)
Do While (s > 0)
    For i4 = 1 To i1 - s
        If Offset(i4)>Offset(i4 + s) Then
            TOffset=Offset(i4 + s)
            TLevel=Design(i4 + s) 
            Design(i4 + s)=Design(i4) 
            Offset(i4 + s)=Offset(i4) 
            Design(i4)=TLevel 
            Offset(i4)=TOffset
        End If
    Next
SngFib = SngFib * SngPhi
s = Int(SngFib)
Loop

Hope it's useful,

David
 
Top